【发布时间】:2013-03-31 22:52:30
【问题描述】:
我一直在使用ThreadLocal 在一端设置值并在另一端使用 get 方法检索值,下面的代码是
public class sample()
{
public ThreadLocal<Object> tl1 = new ThreadLocal<Object>();
protected void Page_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(Myfunction));
setname("myval");
thread.Start();
}
public void setname(String name)
{
tl1.Value = name;
}
public String getname()
{
return (String)tl1.Value;
}
public void Myfunction()
{
String value=getname();
}
}
我已经在 setname 方法中正确设置了值 ..
但发生了什么是当我尝试在单独的线程中获取其值时,即使设置值名称更改为 null。
ex: "myval" changed to "null"
我该如何解决这个问题
【问题讨论】:
标签: c# .net multithreading thread-local