pkolodziej
pkolodziej

Reputation: 1367

Why does the UI thread get blocked when running something in a different thread?

Suppose I have normal button.Click event.

If I try to access UI elements from inside of this event I could potentially got unwanted behavior - or even an exception (when debugging). The usual exception in such a scenario is: ...cannot access UI elements from thread different than they were created in.

Since this is another thread (different than the main) why my UI is blocked when I perform time consuming operations in an event?

Upvotes: 1

Views: 158

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063944

What is the framework here? winform? WPF?

In winform (for a click event), you are on the UI thread. So you can just talk to the UI from the click event. If something else is happening, then there is something wrong. Are you sure you aren't in a timer-callback?

In the more general sense, you can use InvokeRequired/Invoke etc to pass control to the UI thread.

Upvotes: 3

Related Questions