J...
J...

Reputation: 31403

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 there 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?

Upvotes: 1

Views: 350

Answers (2)

David Heffernan
David Heffernan

Reputation: 612964

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.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

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.

Upvotes: 2

Related Questions