I'd like to create a loop thread that runs in parallel from the main thread, so what I did is this code:
ThreadStart rf = delegate () {
while(true)
{
label1_Update();
Thread.Sleep(10);
}
};
GlobalThreadsContainer.LabelUpdateThread = new Thread(rf);
GlobalThreadsContainer.LabelUpdateThread.Name = "Thread_paraLoop";
GlobalThreadsContainer.LabelUpdateThread.Start();
I have a static class with values that I want to access to in the previously referred thread. But I get the inter-thread exception. I understand what it is but I could not find a way to access the static value without throwing this exception. Is there a way to do so or am I constrained to use workaround or to even to use a completely different system to create my non-stopping while loop?
to give more information here is how I retrieve my values:
private void label1_Update()
{
// throws System.InvalidOperationException: inter-thread thingy
label1.Text = GlobalValues.LastMousePosition.ToString();
}
So the idea is to be able to access static values anywhere in my code and especially in parallel threads.