Reputation: 411
I know that some servers dose not allow this, but some does support multiple connections. I am able to download file in parts and combine them after downloading last part , as i am using separate background worker for each filepart...so it is slow and it downloads one file part at a time. I want to start downloading each file part at once. But i don't know how to do this. Tell me which method is more fast and how to use them.
Backgroundworker
Threads
ThreadPool
Thanks for your help.
Upvotes: 3
Views: 9667
Reputation: 9389
If you're downloading your file in HTTP, you can use this method :
http://msdn.microsoft.com/en-us/library/7fy67z6d.aspx
So you'll have to split your file in multiple temp file and then merge them.
But you'll have to be sure that this feature is enabled on the server side (I don't know if it is by default).
And as some said, you'll gain in performance, that's why Free Download Manager is so useful : it download multiple parts of your file in the same time.
To do this with multi threads :
class FileDownloader{
int Start;
int Count;
string PathTemp;
string Url;
FileDownloader(url,start,count){
url = Url;
Start =start;
Count = count;
PathTemp = Path.GetTempFileName()
}
void DoDownload(){
//do your thing with stream and request and save it to PathTemp
}
}
Here is your code to initialize your downloader list :
List<FileDownloader> filewonloadersList = new ListFileDownloader>();
System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://stackoverflow.com/robots.txt");
req.Method = "HEAD";
System.Net.WebResponse resp = req.GetResponse();
int responseLength = int.Parse(resp.Headers.Get("Content-Length"));
for(int i = 0;i<response.Length;i = i + 1024){
filewonloadersList.Add(new FileDownloader("http://stackoverflow.com/robots.txt",i,1024));
}
And your program will initialize X FileDownloader in a list (didn't put this logic here, I'm focusing on the thready stuff)
List<Thread> threadList = new List<Thread>();
foreach(FileDownloader aFildeDownloader in filewonloadersList)
{
Thread aThread = new Thread(aFildeDownloader.DoDownload) //this method will be called when the thread starts
threadList.Add(aThread);
aThread.Start();
}
foreach(Thread aThread in threadList)
{
aThread.Join();//will wait until the thread is finished
}
//all the downloader finished their work now you can go through your downloader list and concatenante the temps files
Upvotes: 11