Reading variables by another thread - variables won't change
So I have a problem here. I declare some variable in my class, then I try
to change it and then read it with my windows form thread. The variable is
being read as the initial value that it was declared with, and nothing can
change it. Example:
I declare the value in my Class1.cs:
public static int TestVar = 51;
Then I change the value later in that same class to for example 45:
TestVar = 45;
Then I read the variable from my windows form thread like this:
label1.Text = Class1.TestVar.ToString();
And the result in my windows form is 51 instead of 45. It doesn't matter
if I declare it to 0 or without any initial value. It will just stay as 0
instead.
The class it has been declared it reads the variable fine as 45, somehow
the other thread doesn't have it updated.
The windows form is being run using:
public void RunThread()
{
Thread thread = new Thread(new ThreadStart(RunForm));
thread.Name = "NewForm";
thread.Start();
}
public void RunForm()
{
Application.Run(new NewForm());
}
Any help?
No comments:
Post a Comment