1

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?

J...
  • 30,968
  • 6
  • 66
  • 143
  • 1
    See http://stackoverflow.com/questions/747210/whats-wrong-with-calling-invoke-regardless-of-invokerequired – stuartd Sep 01 '11 at 12:19
  • @ Stuart - thanks. I added tags to that question to make it more searchable. – J... Sep 01 '11 at 12:30

2 Answers2

2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • It seemed to work just fine so I figured as much. It is for an embedded control system so there is a pretty ironclad guarantee that nothing else is ever running on the system but this one application. I suppose it would probably still be best practice to do it anyway, even if I have orders more processing headroom than I need. – J... Sep 01 '11 at 12:34
2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • If that article is correct, it indeed makes a strong and interesting point : "In fact, the Invoke method will "do the right thing" in that case, and simply invoke the target delegate directly rather than trying to marshall it onto the thread that owns the Control instance." Can anyone refute this? Does this not further weaken the case for InvokeRequired being... required? – J... Sep 01 '11 at 13:36