I want to send a file in chunks by calling a function that calls a webservice using multithreading.
The following is a brief of the code:
int chunkSize = "whatever in byte";
byte[] fileBytes = ConvFileToByte("the pathe of the file");
int numberOfParts = (int)Math.Ceiling((decimal)fileSize / chunkSize);
for (int i; i< numberOfParts; i++)
{
//Get the offset.
//Get the bytes to send.
SendFile(ByteToSend, offset) // This call a method in a webservice.
}
What is the best way to use mutithreading in this function?
Note: but don’t forget that if one chunk failed to send I should send it again.
Unless you have multiple instances of your webservice under some kind of loadbalancing AND your upload bandwith is higher than the download capabilities of each single service host, it does not make sense to multithread your call if you want to achieve higher upload speeds.
On the other hand, if the file you want to upload is very large and you want to limit the amount of memory used to buffer the file, then chunking makes sense. If this is your case and if you have control over the webservice implementation, you should consider using WCF chunking instead of writing your own chunking mechanism.