L
linuxfedora
When i try to do something like that:
Button On Click->Call ShowInfo();
class testing
{
private Object locker= new Object();
public void ShowInfo()
{
Console.WriteLine("Begin");
using(locker)
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine("j=" + j);
}
}
Console.WriteLine("End");
}
}
then when i click the button very quick, the result will be like that:
Begin
j=0;
j=1;
Begin
j=0;
Begin
but the what the result i wanted is
Begin
j=0;
j=1;
j=2;
j=3;
j=4;
j=5;
j=6;
j=7;
j=8;
j=9;
End
Begin
j=0
..
..
What is wrong with my code? and how to do the same thing?
How to ensure some codes are completed before another thread is trying
to enter the code?Thanks
Button On Click->Call ShowInfo();
class testing
{
private Object locker= new Object();
public void ShowInfo()
{
Console.WriteLine("Begin");
using(locker)
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine("j=" + j);
}
}
Console.WriteLine("End");
}
}
then when i click the button very quick, the result will be like that:
Begin
j=0;
j=1;
Begin
j=0;
Begin
but the what the result i wanted is
Begin
j=0;
j=1;
j=2;
j=3;
j=4;
j=5;
j=6;
j=7;
j=8;
j=9;
End
Begin
j=0
..
..
What is wrong with my code? and how to do the same thing?
How to ensure some codes are completed before another thread is trying
to enter the code?Thanks