I'm trying to start a number of threads at the same time but it is not working. Heres my code:
var previous = 0;
var threadList = new List<Thread> {};
for (var i = (int)partSize; i <= responseLength; i = i + (int)partSize)
{
var t = new Thread(() => Download(URL, previous, i));
//t.Name = i.ToString();
threadList.Add(t);
//t.Start();
//t.Join(300);
//new Thread(() => {Download(URL,previous, i);}).Start();
//var t = Task.Factory.StartNew(() => Download(URL, previous, i));
previous = i;
}
foreach (Thread t in threadList)
{
//Console.WriteLine(t.Name);
t.Start();
}
OUTPUT:
77296,86958
77296,86958
77296,86958
77296,86958
77296,86958
77296,86958
77296,86958
77296,86958
After displaying this for a while, it hangs up and eventually crashes.
Expected output with different code:
for (var i = (int)partSize; i <= responseLength; i = i + (int)partSize)
{
var copy = previous;
var t = new Thread(() => Download(URL, copy, i));
t.Start();
t.Join();
previous = i;
}
OUTPUT:
0,9662
9662,19324
19324,28986
28986,38648
38648,48310
48310,57972
57972,67634
67634,77296
As for the output the first number indicates where to start downloading a piece of a file and the second number indicates where to finish (in bytes) How can I start every thread in the list, with the parameters I am assigning to it? I am lost, so any help would be awesome. Thanks!