I am trying to set up file uploading to a server.
I would like to divide the file into 'n' chunks of 'y' size each and then upload 'x' chunks at a time to the server using threads. When one of the 'x' chunks is completed then another chunk should start uploading until there are no more chunks left to upload.
I have been looking at BackgroundWorker and have the following implementation idea:
1) The following method to assign to 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) I can then 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);
With backgroundWorker, I can have it report progress as well and can also be notified about completion in order to start uploading the next chunk, so those are a couple of reasons why I would like to use it. The problem is that I am not sure if I should call worker.RunWorkerAsync() in the following manner (in case that I am uploading 4 chunks at a time):
worker.RunWorkerAsync(argumentsListForChunk1);
worker.RunWorkerAsync(argumentsListForChunk2);
worker.RunWorkerAsync(argumentsListForChunk3);
worker.RunWorkerAsync(argumentsListForChunk4);
And then when any of the worker completes and gives me a worker completed call back, then I can do this:
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);
}
I am relatively new to c# and would like to know if this is a good way to get things done.
My requirements, in a nutshell, are
- Use threads to upload file chunks
- Limit the number of threads
- Keep a check for completed threads so that we can start uploading the next file chunk
- Keep track of thread progress.
Any help will be appreciated :)
Edit:: The comments form @o_weisman and @HansPassant made me realize that the question was missing the following relevant information. I am trying to upload to a server that I control. The server limits connection speed and thus opening multiple connections for uploading file chunks will result in an over all speed boost.
Thanks!