I found this code:
this.Invoke(new EventHandler(EventGetSum));
Is this not the same as writing:
EventGetSum();
What's the use of this?
The Invoke method searches up the control's parent chain until it finds a control or form that has a window handle if the current control's underlying window handle does not exist yet. If no appropriate handle can be found, the Invoke method will throw an exception.
Invoke is used of putting into effect or calling upon such things as laws, authority, or privilege (“the principal invoked a rule forbidding students from asking questions”). Evoke is primarily used in the sense “to call forth or up” and is often found in connection with such things as memories, emotions, or sympathy.
InvokeRequired is checking to see if it's on the UI thread if not it equals True , Me. Invoke is asking for a delegate to handle communication between the diff threads. As for your side note. I typically use an event to pass data - this event is still on the diff thread, but like above you can delegate the work.
this. Invoke: This is a synchronization mechanism, contained in all controls. All graphic/GUI updates, must only be executed from the GUI thread. (This is most likely the main thread.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With