Is the winforms event handler running on the same thread as the caller?

A simple question, although no one in the office seems to know, and I cannot find a good way to ask Google. In winforms, if you have a function that processes an event (in this case, focusLost), does this function perform the same thread as the one that triggered the event?

So, if I have a focus text box that is currently running in the user interface thread and I change focus, will the user interface thread perform my function?

+4
source share
2 answers

Yes, the UI thread will execute the UI event handlers.

Generally, when programming Windows, you should not touch UI components on other threads. Windows Forms is designed to work through a single thread. If you need to lift it heavily, which otherwise could freeze the user interface thread, you create a new thread to do the work, and then make changes to the user interface thread.

You can use SynchronizationContext.Current to publish work to the user interface thread. BackgroundWorker is also suitable for this.

+4
source

I think this is correct. Typically, events are processed in a GUI thread. Below is a link on how to do this in another thread.

http://bytes.com/topic/c-sharp/answers/526484-handling-control-ui-events-worker-threads

+1
source

All Articles