Why bother with InvokeRequired
I understand the need to use Invoke/BeginInvoke to make calls from worker threads to functions or procedures which make changes to components which belong to the UI thread...
My question is - is 开发者_如何学Gothere a good reason to bother checking InvokeRequired? Say an event handler may be called either from a worker thread or from the UI thread in different circumstances. Are there any consequences to having the UI thread call Invoke on a method which it will itself end up executing?
Say...
Private Sub SomeProcedure() Handles event1, event2, event3
Me.Invoke(New delegateSomeProc(Address of SomeProc))
EndSub
Now, say event1 fires from the UI thread but Events 2 and/or3 fire from some other thread... is there any peril to event1 calling invoke anyway, even though it doesn't have to? Is it just a bit inefficient?
Are there any consequences to having the UI thread call Invoke on a method which it will itself end up executing?
The only difference that I know of is that using Invoke
will fail if called before the control's handle has been created.
This article discusses the issues in more detail.
Are there any consequences to having the UI thread call Invoke on a method which it will itself end up executing?
No, there are no consequences, other than probably performance as if no invoke is required, a direct method call will be faster than passing through the Invoke infrastructure.
精彩评论