1

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?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
pkolodziej
  • 1,347
  • 3
  • 17
  • 24

1 Answers1

3

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • How can you tell that some events run in UI thread and others not ? – pkolodziej Apr 02 '09 at 11:52
  • 1
    By reading the documentation for the event. Most UI-related events will fire on the UI thread. If unsure, check InvokeRequired. – Marc Gravell Apr 02 '09 at 12:16
  • If the Event fires in thread OTHER than UI it will not prevent window from being refreshed but if it will fire in UI thread the window will not be redrawn until event closes? – pkolodziej Apr 02 '09 at 13:10