ProAnswers.org

Simplest way to update an label from another thread in C#?

I have a winform(thread1), from that I’m starting another thread(thread2). While thread2 is processing some files I would like to update a label on the winform, with status from thread2.

How can I do that?

///…some updating files
string newText = “abc”; // running on worker thread
this.Invoke((MethodInvoker)delegate {
someLabel.Text = newText; // runs on UI thread
});
/// more updating files

The simple solution is to use Control.Invoke.

			void DoSomething() 
			{ 
			    if (InvokeRequired) { 
			        Invoke(new MethodInvoker(DoSomething)); 
			    } else { 
			        // Do Something 
			    } 
			}