I am starting atmost 5 threads in a program in C# .NET. But sometimes some of those threads just quit or become dead inexplicably even before the completion of execution of the function assigned to it.
It happens randomly . If I try to debug the code by putting breakpoints- It works fine.
And sometimes all the threads execute the assigned functions perfectly.They do not share any resources among them.
Thread[] td = new Thread[5];
for (count = 4; count >= 0; --count)
{
ds[count] = dba.getData(ru[count]);
td[count] = new Thread(delegate() { runRule[count].performTask(ru[count], ds[count], count); });
td[count].Name = "Thread " + count.ToString();
td[count].Start();
Thread.Sleep(50);
}
If I remove the last line "Thread.Sleep(50)" only the first thread stared runs and rest of them just die.
can someone explain why the threads are becoming dead?