Reputation: 11415
I have created a usercontrol in VB.NET, and I am doing some rather time-intensive stuff in the MouseMove event.
I expected the MouseMove events to be queued.
But I included a trap, and it shows me that the MouseMove event indeed is not queued, instead it works async, I think.
Private _bInproc As Boolean = false
Private Sub ucGrid_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If _bInproc Then
Stop 'This should never be reached if MouseMove events were always queued, I think
End If
_bInproc = True
doMousePointerMoveStuff(e.X, e.Y)
_bInproc = False
End Sub
Does anybody see any mistakes that I could have done, or does anybody know a situation where it could become async as in my case??
Upvotes: 0
Views: 765
Reputation: 43743
WinForm events such as MouseMove are not multithreaded (async). The events are always raised in on the same UI thread in a serial fashion. However, that is only true of the events when they are raised by the window message loop (WndProc). That's they typical way that the events are raised, but you can, in your own code, cause the event to be re-raised before the first event is done being handled. There are several ways in which that can happen. First, from within the event handler, you could recursively call the same event handler method. Secondly, from within the event handler, you could raise the same event again, which will cause the event handlers to be called again immediately. Thirdly, you could call DoEvents which would cause the message loop to immediately process all of the queued window messages which might raise the same event again. All of these things are confusing and unexpected, so I would discourage doing any of those things. This is one of the primary reasons why DoEvents is so discouraged. In order to figure out where it is getting called from, just put a break point in the event handler and look at the call stack each time it gets called.
Upvotes: 2
Reputation: 26454
Events in WinForms are never async, they always happen on the UI thread. This is why you are able to change other controls' properties in them. Otherwise you'd be getting cross-thread operation exception.
Now because they are synchronous with UI thread, there cannot be any event queue. You cannot send another event from the UI thread, i.e. by moving a mouse, because UI thread is currently processing the event handler.
You can have your own async event handler, in which case you are responsible for handling thread synchronization in it.
Hope it makes sense. If not, please ask in comments below.
Upvotes: 0