tags:

views:

248

answers:

4

I found this code:

this.Invoke(new EventHandler(EventGetSum));

Is this not the same as writing:

EventGetSum();

What's the use of this?

+10  A: 

If you write EventGetSum() that immediately calls the EventGetSum method.

If you write new EventHandler(EventGetSum) that creates a delegate which will (in turn) call EventGetSum when it's invoked.

The call to Control.Invoke invokes the given delegate from the UI thread responsible for the control. This is necessary because you mustn't access UI elements from arbitrary threads.

Jon Skeet
+4  A: 

It executes the EventGetSum method in the thread to that the window this belongs to.

Timbo
+3  A: 

This is normally used when dealing with cross thread UI calls.

Look at the MSDN documentation for ISynchronizeInvoke.

leppie