Click here to Skip to main content
11,796,275 members (77,791 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET Threading
hi i work on a project for download a large file in multiconnection.i used this code but have some problems and didn't work connections together.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            List<FileDownloader> filewonloadersList = new List<FileDownloader>();
       System.Net.WebRequest req = System.Net.HttpWebRequest.Create("");
            req.Method = "HEAD";
            System.Net.WebResponse resp = req.GetResponse();
            int responseLength = int.Parse(resp.Headers.Get("Content-Length"));
            int downloadparts = responseLength / 5;
            for (int i = 0; i < responseLength; i = i + downloadparts)
                FileDownloader filedownloadlistitem = new FileDownloader();
                filedownloadlistitem.Start = i;
                filedownloadlistitem.End = i + downloadparts;
                filedownloadlistitem.partnumber = filewonloadersList.Count + 1 ;
                filedownloadlistitem.Url = req.RequestUri.ToString();
                List<Thread> threadList = new List<Thread>();
                for (int i = filewonloadersList.Count-1; i >= 0; i--)
                    ThreadStart ts = new ThreadStart(filewonloadersList[i].DoDownload);
                    Thread run = new Thread(ts);

                //foreach (Thread aThread in threadList)
                //  //will wait 

and Filedownloader.cs is:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
namespace ConsoleApplication1
        public class FileDownloader{
            public int Start;
            public int End;
            public string Url;
            public object partnumber;
           public  void DoDownload(){
                // Create a New 'HttpWebRequest' object .
               HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                myHttpWebRequest.AddRange(Start, End);
                //Console.WriteLine("Call AddRange("+Start+","+End+")");
                //Console.Write("Resulting Request Headers: ");

                // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
                HttpWebResponse myHttpWebResponse=null;
                while (myHttpWebResponse == null)
                        myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                    catch { }
                // Displays the headers in the response received
                //Console.Write("Resulting Response Headers: ");
               // Console.WriteLine(myHttpWebResponse.Headers.ToString());

                // Display the contents of the page to the console.
                Stream streamResponse = myHttpWebResponse.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                Char[] readBuffer = new Char[256];
                int count = streamRead.Read(readBuffer, 0, 256);
               // Console.WriteLine("\nThe HTML contents of the page from "+Start+" to "+End+" characters are :\n  ");
                TextWriter write2file = new StreamWriter("c:\\testdownloadfile" + Convert.ToString(partnumber) + ".txt", true); 
                    while (count > 0)
                    String outputData = new String(readBuffer, 0, count);
                    count = streamRead.Read(readBuffer, 0, 256);
                // Release the response object resources.

how can i solve it?plz help!ik
Posted 3-Apr-13 11:38am
lewax00 at 3-Apr-13 17:47pm
You'll need to be more specific than "have some problems and didn't work connections together". How is it not working exactly? Specifically, what do you expect it to do, and what is it actually doing? Also, include any error messages, if there are any.
mohsenadc at 3-Apr-13 18:01pm
It should start download functions (in filewonloadersList list)together.
In the other word , cpu just do a theard but i need to run all theards.
lewax00 at 3-Apr-13 19:56pm
I'm not sure I understand the problem, is only one thread completing? If so, have you made sure a) all threads have actually finished executing and b) there are no errors in the other threads?
mohsenadc at 4-Apr-13 12:32pm
first thread (in list of threads)do dodownload function and after it finished ,next thread start a download manager application like IDM ,connections start together but in this code connection are in an queue.
lewax00 at 4-Apr-13 16:41pm
Have you confirmed it's not the server? e.g. is the server only sending data to one thread at a time? Some servers only allow one file download at a time.
mohsenadc at 6-Apr-13 0:28am
I tested server by a download manager(IDM) and download started with 6 connections.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 460
1 Abhinav S 368
2 OriginalGriff 340
3 CPallini 269
4 Sergey Alexandrovich Kryukov 252
0 OriginalGriff 2,012
1 Maciej Los 1,615
2 KrunalRohit 1,310
3 CPallini 1,015
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100