views:

46

answers:

2

Hi

Language: C# (WinForms)

There is one think I don't fully understand:

Suppose I have normal button.Click event. I know for sure from my own experience that if I try to access UI elements from inside of this event I could potentially got unwanted behavior - or even 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?

Could you please help me out with this? PK

A: 

See this question.

Anton Gogolev
+1  A: 

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
How can you tell that some events run in UI thread and others not ?
pkolodziej
By reading the documentation for the event. Most UI-related events will fire on the UI thread. If unsure, check InvokeRequired.
Marc Gravell
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
exactly (sorry for delay)
Marc Gravell