I want to make a multi threaded downloader, and each thread needs to have access to the output stream and needs a independent stream object with a independent stream position.
With filestreams i can easily archieve this through FileShare.ReadWrite and opening a independent stream instance for each thread but how can i archieve the same with normal streams? This is how the multitheaded download works using multiple filestreams:
// calculate the download chunk size
var loops = (int)Math.Ceiling((double)total / chunksize);
int[] nums = new int[loops];
for (int i = 0; i < loops; i++) nums[i] = i;
// run 8 download threads in parralel
// each thread downloads a chunk to the same file
await Util.ParallelForEachAsync(nums, (chunkIndex) =>
{
var currentPos = (chunkIndex * chunksize);
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, url);
message.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(currentPos, currentPos + chunksize - 1);
// each thread is able to get a independent filestream object. How can i archieve this with normal streams?
using (FileStream f = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
// write message content to stream
await ContentToStreamAsync(f, message, onProgress);
}
}, 8);
With this downloading method i am able to archive incredible downloadspeeds compared to a single theaded download.
My question is how i can archieve the same with a stream (instead of filestream)? This is difficult because normal streams dont support a ShareMode and therefore cant be shared with multiple threads.