When we try to update the UI from a different thread it throws an exception saying The calling thread cannot access this object because a different thread owns it
. OK, that's fine to me.
But why doesn't the below mentioned code throw a similar exception? It's possible that thread t1 and t2 are writing to value
at very same time.
public class myclass
{
string value;
public myclass()
{
Thread t1 = new Thread(method1);
t1.Start();
Thread t2 = new Thread(method2);
t2.Start();
}
public void method1()
{
while (true)
{
value = "method1";
}
}
public void method2()
{
while (true)
{
value = "method2";
}
}
}