开发者

Send file in chunks using multithreading

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 we开发者_JS百科bservice.
}

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.


If your web service is safe to receive the chunks out of order, consider using the ThreadPool class (see an excellent example here: VB.Net Threading). You will be able to set the number of parallel threads etc.

Re-sending can be done inside the function that uploads a single chunk. Say, if the request fails (does not get the "200" response from server), you start over again (will have to count times you have retried, otherwise an ifinite loop is possible).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜