I am trying to download a file in multiple parts simultaneously for that I am using threads
for (int i = 0; i < 5; i++)
{
try
{
Thread chunkThread = new Thread(() =>
{
Console.WriteLine("try_index" + i);
});
chunkThread.Start();
}
catch (Exception e)
{
Console.WriteLine("catch_index" + i);
}
if (i == 0)
{
Console.WriteLine("here");
}
}
since downloading works fine I cleared that code when I watch the Console.Writelines it always writes wrong I thought its gonna write
try_index0
here
try_index1
try_index2
try_index3
try_index4
but the result is
here
try_index1
try_index2
try_index4
try_index5
try_index5
or something like that but always not I wanted and always a 5 involves for parts of the download I keep a list with a start and end bytes of the parts and since it gets 5 my code crashes it is something about new thread but I am too noob to understand how can I fix ?