DEV Community

1001binary
1001binary

Posted on

6 1

Update UI state from an another Thread in Windows Forms

An exception is thrown when trying to update control state from an another Thread.

Thread newThread = new Thread(new ThreadDelegate(() => {
   txtStatus.Text = "Started";
}));

newThread.Start();
Enter fullscreen mode Exit fullscreen mode

Solution

We use BeginInvoke to update UI state asynchronously.

Thread newThread = new Thread(new ThreadDelegate(() => {
   txtStatus.BeginInvoke(() =>
   {
       txtStatus.Text = "Started";
   });
}));

newThread.Start();
Enter fullscreen mode Exit fullscreen mode

Happy coding!

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay