I am running a thread in C#. I have web form. I declared public string attribute in my web form. (e.g. string myVal) Then I called thread and assign value into myVal. It assign values in it. But when I exit from thread code, myVal become null.
我在C#中运行一个线程。我有网络表格。我在网络表单中声明了公共字符串属性。 (例如字符串myVal)然后我调用了线程并将值赋值给myVal。它在其中分配值。但是当我退出线程代码时,myVal变为null。
Is there anyway to keep myVal value.
无论如何都要保持myVal值。
public string myVal;
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread myThread = new System.Threading.Thread(this.getVal);
myThread.SetApartmentState(System.Threading.ApartmentState.STA);
myThread.Start();
myThread.Join();
//I am not able to get myVal string over here.
}
private void getVal()
{
myVal = "I can easily get myVal over here.";
}
4 个解决方案
#1
Test Case failure: I copy-pasted your code in a new ASP.NET Project and added, after the myThread.Join() :
测试用例失败:我将代码复制粘贴到新的ASP.NET项目中,并在myThread.Join()之后添加:
Label1.Text = myVal;
And the label does show your string.
标签确实显示了你的字符串。
#2
Do you reference myVal further in your code or are you just inspecting it with the debugger and checking the value after you are done with it? If so maybe the garbage collector has already gotten to it.
您是否在代码中进一步引用了myVal,或者您只是使用调试器检查它并在完成后检查它?如果是这样,垃圾收集器可能已经开始了。
#3
In general what you seem to be doing is trying to keep a value around after the thread it was created on has exited.
一般来说,你似乎正在做的是尝试在创建它的线程退出后保持一个值。
I would recommend using an App Domain(MSDN). In short, an all threads sit inside an AppDomain (for more info visit the link) and you already have one by the nature of the program.
我建议使用App Domain(MSDN)。简而言之,所有线程都位于AppDomain中(有关更多信息,请访问该链接),并且您已经拥有了该程序的性质。
So what you would do in your situation is:
所以你在你的情况下会做的是:
To "save" the data: AppDomain.CurrentDomain.SetData("val", myVal);
要“保存”数据:AppDomain.CurrentDomain.SetData(“val”,myVal);
To retrieve the data: AppDomain.CurrentDomain.GetData("val");
要检索数据:AppDomain.CurrentDomain.GetData(“val”);
Hope that helps
希望有所帮助
#4
My guess is you are getting "System.InvalidOperationException: Cross-thread operation not valid" in the getVal method. You could put a try catch around it to verify that an exception is being thrown. I would suggest using a BackgroundWorker or other form of callback.
我的猜测是你在getVal方法中得到“System.InvalidOperationException:跨线程操作无效”。你可以试一试它来验证是否抛出异常。我建议使用BackgroundWorker或其他形式的回调。
#1
Test Case failure: I copy-pasted your code in a new ASP.NET Project and added, after the myThread.Join() :
测试用例失败:我将代码复制粘贴到新的ASP.NET项目中,并在myThread.Join()之后添加:
Label1.Text = myVal;
And the label does show your string.
标签确实显示了你的字符串。
#2
Do you reference myVal further in your code or are you just inspecting it with the debugger and checking the value after you are done with it? If so maybe the garbage collector has already gotten to it.
您是否在代码中进一步引用了myVal,或者您只是使用调试器检查它并在完成后检查它?如果是这样,垃圾收集器可能已经开始了。
#3
In general what you seem to be doing is trying to keep a value around after the thread it was created on has exited.
一般来说,你似乎正在做的是尝试在创建它的线程退出后保持一个值。
I would recommend using an App Domain(MSDN). In short, an all threads sit inside an AppDomain (for more info visit the link) and you already have one by the nature of the program.
我建议使用App Domain(MSDN)。简而言之,所有线程都位于AppDomain中(有关更多信息,请访问该链接),并且您已经拥有了该程序的性质。
So what you would do in your situation is:
所以你在你的情况下会做的是:
To "save" the data: AppDomain.CurrentDomain.SetData("val", myVal);
要“保存”数据:AppDomain.CurrentDomain.SetData(“val”,myVal);
To retrieve the data: AppDomain.CurrentDomain.GetData("val");
要检索数据:AppDomain.CurrentDomain.GetData(“val”);
Hope that helps
希望有所帮助
#4
My guess is you are getting "System.InvalidOperationException: Cross-thread operation not valid" in the getVal method. You could put a try catch around it to verify that an exception is being thrown. I would suggest using a BackgroundWorker or other form of callback.
我的猜测是你在getVal方法中得到“System.InvalidOperationException:跨线程操作无效”。你可以试一试它来验证是否抛出异常。我建议使用BackgroundWorker或其他形式的回调。