Call BackgroundWorker.RunWorkerAsync () several times with various parameters

I am trying to configure uploading files to a server.

I would like to split the file into "n" chunks of size "y" each, and then upload the "x" chunks at a time to the server using streams. When one of the "x" blocks is complete, the other piece should start loading until there are no more pieces left to load.

I looked at BackgroundWorker and have the following implementation idea:

1) The next way to assign Worker.DoWork

private void ChunkUploaderDoWork(object sender, DoWorkEventArgs e) {
    //get arguments list form e.arguments and prepare next upload
    //The arguments list contains information such as next chunk number and chunk size etc..
    //Calculate file stream offset based on chunk size and upload the next y bytes.
}

2) Then I can call

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, e) => WorkerDoWork();
worker.ProgressChanged += new ProgressChangedEventHandler (Worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler (Worker_RunWorkerCompleted);
worker.RunWorkerAsync(argumentsList);

backgroundWorker , , , , . , , worker.RunWorkerAsync() ( , 4 ):

worker.RunWorkerAsync(argumentsListForChunk1);
worker.RunWorkerAsync(argumentsListForChunk2);
worker.RunWorkerAsync(argumentsListForChunk3);
worker.RunWorkerAsync(argumentsListForChunk4);

, - , :

void Worker_RunWorkerCompleted(object sender, ProgressChangedEventArgs e)
{
    //Check if we have any outstanding chunks and upload next chunk if available.
    //for example if we had chunk 5 next in line, then I could call RunWorkerAsync as follows:
    worker.RunWorkerAsync(argumentsListForChunk5);
}

# , .

, ,

  • ,
  • .

:)

Edit:: @o_weisman @HansPassant , . , . , , , .

!

+4
1

BackgroundWorker . .

.
1. - .
2. , , Task factory, .
3. , , , , , , .
4. . , . , - .

: http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx

+2

All Articles