ThreadStatic
private void foo()
{
var t = new Thread(x =>
{
//MyClass.MyProperty = 9;
MessageBox.Show(MyClass.My.I.ToString());
});
t.Start();
}
public class MyClass
{
[ThreadStatic]
public static int MyProperty = 5;
[ThreadStatic]
public static MyClass My = new MyClass();
public int I = 1;
}
{
var t = new Thread(x =>
{
//MyClass.MyProperty = 9;
MessageBox.Show(MyClass.My.I.ToString());
});
t.Start();
}
public class MyClass
{
[ThreadStatic]
public static int MyProperty = 5;
[ThreadStatic]
public static MyClass My = new MyClass();
public int I = 1;
}
threadstatic 类型标识的变量不需要给其赋值,不同的线程对它进行访问的时候,只在第一次会发生作用,
在被第一次访问后,被重置为类型的默认值,
第一次执行正常: MessageBox.Show(MyClass.My.I.ToString());
第二次执行:异常了。