System.Windows.Forms.Control.Invoke Method

Executes the specified delegate on the thread that owns the control's underlying window handle.

Syntax

public object Invoke (Delegate method)

Parameters

method
A delegate that contains a method to be called in the control's thread context.

Returns

The return value from the delegate being invoked, or null if the delegate has no return value.

Remarks

Delegates are similar to function pointers in C or C++ languages. Delegates encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code that calls the referenced method, and the method to be invoked can be unknown at compile time. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and more secure.

The Control.Invoke(Delegate) 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 Control.Invoke(Delegate) method will throw an exception. Exceptions that are raised during the call will be propagated back to the caller.

Note:

In addition to the Control.InvokeRequired property, there are four methods on a control that are thread safe: Control.Invoke(Delegate), Control.BeginInvoke(Delegate), Control.EndInvoke(IAsyncResult), and Control.CreateGraphics if the handle for the control has already been created. Calling Control.CreateGraphics before the control's handle has been created on a background thread can cause illegal cross thread calls. For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread.

The delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. The delegate can also be an instance of System.Windows.Forms.MethodInvoker, or any other delegate that takes a void parameter list. A call to an EventHandler or System.Windows.Forms.MethodInvoker delegate will be faster than a call to another type of delegate.

Note:

An exception might be thrown if the thread that should process the message is no longer active.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0