C
Cool Guy
In the following, why isn't there a pause when trying to access c in the
statement "Console.WriteLine(c.i);"?
I thought that c would be locked and therefore not accessible at the time.
Why isn't this the case?
using System;
using System.Threading;
class C
{
public int i = 0;
static void Main()
{
C c = new C();
Thread thread = new Thread(new ThreadStart(c.Foo));
thread.Start();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(c.i);
}
Console.Read();
}
void Foo()
{
lock (this)
{
Thread.Sleep(10000);
i++;
}
}
}
statement "Console.WriteLine(c.i);"?
I thought that c would be locked and therefore not accessible at the time.
Why isn't this the case?
using System;
using System.Threading;
class C
{
public int i = 0;
static void Main()
{
C c = new C();
Thread thread = new Thread(new ThreadStart(c.Foo));
thread.Start();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(c.i);
}
Console.Read();
}
void Foo()
{
lock (this)
{
Thread.Sleep(10000);
i++;
}
}
}