I found this code:
this.Invoke(new EventHandler(EventGetSum));
Is this not the same as writing:
EventGetSum();
What's the use of this?
I found this code:
this.Invoke(new EventHandler(EventGetSum));
Is this not the same as writing:
EventGetSum();
What's the use of this?
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.
It executes the EventGetSum
method in the thread to that the window this
belongs to.
This is normally used when dealing with cross thread UI calls.
Look at the MSDN documentation for ISynchronizeInvoke.
Check this question http://stackoverflow.com/questions/571706/shortest-way-to-write-a-thread-safe-access-method-to-a-windows-forms-control/571749#571749