Search code examples
c#user-interfaceeventsinterruption

Is it possible that GUI event interrupts running code from the GUI thread to execute its own event handler method?


I have a weird behavior in my GUI code. If the user produces a lot of events in a short time, it happens that a running event handler method gets interrupted by another event handler method. Since everything runs in the same thread (GUI thread) everything should run sequential and an interruption should not be possible, or do I misunderstand something?

Thanks for your advise, Eny


Solution

  • No, that doesn't happen. You are right in your understanding that the thread runs sequentially.

    The GUI thread can be interrupted but only to run a different thread, it will not re-enter the GUI thread to handle another event. A thread only has one instruction pointer and thus can only be in one place in the code, it can not be interrupted by itself.

    If you are experiencing something that look like the GUI thread is re-entered, the reason is something else.

    The GUI thead can however "interrupt" itself by calling the Application.DoEvents method.