Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of Invoke() in .net?

Tags:

c#

.net

invoke

I found this code:

this.Invoke(new EventHandler(EventGetSum));

Is this not the same as writing:

EventGetSum();

What's the use of this?

like image 918
Gold Avatar asked Feb 27 '09 10:02

Gold


People also ask

What is invoke () in C#?

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.

Why invoke is used?

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.

What is invoke required vb net?

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.

What is this invoke?

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.)


1 Answers

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.

like image 179
Jon Skeet Avatar answered Sep 29 '22 21:09

Jon Skeet