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.
Is there anyway to keep myVal value.
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.";
}
Test Case failure: I copy-pasted your code in a new ASP.NET Project and added, after the myThread.Join() :
And the label does show your string.