Program.cs:
class Program
{
static void Main()
{
Class1 c1 = new Class1();
for(int i=0;i<2;i++)
{
Thread t = new Thread(() =>
{
c1.pr(i);
});
t.Start();
}
}
}
Class1.cs:
public class Class1
{
public void pr(int i)
{
Console.WriteLine((i).ToString());
}
}
Result: 2 2
I want the result to be 0 1. For each thread, value of i is assigned to function 'pr'.
If i add t.Join()
after t.Start()
it works. But the joined thread has to wait until other threads have done their works, is there any solution that provide the same result without using Join() function? I want to execute them at a same time