I have a thread that runs in parallel with the Main Form (UI). All it does (for now) is increment a counter every second. I want to display the value of the counter using a label in Windows Forms. Is that possible? When I try the following code, I get a compile error in the ShowValue method. I have to declare ShowValue "static" so that I can call it from the background thread. But if I do that, I cannot use the "this." to access the label in ShowValue Form1. Is this the correct way to do this? Any tip would be appreciated, thanks!
private void count_secs()
{
while (!stopThread)
{
if (stopThread)
{
break;
}
num2++; // increment counter
Form1.ShowValue(num2); // display the counter value in the main Form
try
{
Thread.Sleep(1000); // wait 1 sec.
}
catch (ThreadInterruptedException)
{
if (stopThread)
{
break;
}
}
}
}
Then in my Form1 class, I have:
public static void ShowValue(int num)
{
this.label7.Text = num.ToString();
// compiler error here: "Keyword 'this' is not valid in a static method.
}