Control.BeginInvoke Method

Definition

Executes a delegate asynchronously on the thread that the control's underlying handle was created on.

Overloads

BeginInvoke(Delegate, Object[])

Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on.

BeginInvoke(Action)

Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

BeginInvoke(Delegate)

Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

BeginInvoke(Delegate, Object[])

Source:
Control.cs
Source:
Control.cs
Source:
Control.cs

Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on.

public IAsyncResult BeginInvoke (Delegate method, object[] args);
public IAsyncResult BeginInvoke (Delegate method, params object[] args);
public IAsyncResult BeginInvoke (Delegate method, params object?[]? args);

Parameters

method
Delegate

A delegate to a method that takes parameters of the same number and type that are contained in the args parameter.

args
Object[]

An array of objects to pass as arguments to the given method. This can be null if no arguments are needed.

Returns

An IAsyncResult that represents the result of the BeginInvoke(Delegate) operation.

Implements

Exceptions

No appropriate window handle can be found.

Examples

The following code example demonstrates a use of the BeginInvoke method.

public delegate void MyDelegate(Label myControl, string myArg2);

private void Button_Click(object sender, EventArgs e)
{
   object[] myArray = new object[2];

   myArray[0] = new Label();
   myArray[1] = "Enter a Value";
   myTextBox.BeginInvoke(new MyDelegate(DelegateMethod), myArray);
}

public void DelegateMethod(Label myControl, string myCaption)
{
   myControl.Location = new Point(16,16);
   myControl.Size = new Size(80, 25);
   myControl.Text = myCaption;
   this.Controls.Add(myControl);
}

Remarks

The delegate is called asynchronously, and this method returns immediately. You can call this method from any thread, even the thread that owns the control's handle. If the control's handle does not exist yet, this method searches up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, BeginInvoke will throw an exception. Exceptions within the delegate method are considered untrapped and will be sent to the application's untrapped exception handler.

You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. EndInvoke will block until the return value can be retrieved.

Note

Most methods on a control can only be called from the thread where the control was created. In addition to the InvokeRequired property, there are four methods on a control that are thread safe: Invoke, BeginInvoke, EndInvoke, and CreateGraphics if the handle for the control has already been created. Calling 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 invoke methods always invoke their callbacks on the control's thread.

Note

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

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

BeginInvoke(Action)

Source:
Control.cs
Source:
Control.cs
Source:
Control.cs

Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

public IAsyncResult BeginInvoke (Action method);

Parameters

method
Action

A delegate to a method that takes no parameters.

Returns

An IAsyncResult that represents the result of the BeginInvoke(Action) operation.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 6, 7, 8, 9

BeginInvoke(Delegate)

Source:
Control.cs
Source:
Control.cs
Source:
Control.cs

Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

public IAsyncResult BeginInvoke (Delegate method);

Parameters

method
Delegate

A delegate to a method that takes no parameters.

Returns

An IAsyncResult that represents the result of the BeginInvoke(Delegate) operation.

Exceptions

No appropriate window handle can be found.

Examples

The following code example demonstrates a use of the BeginInvoke method.

public delegate void InvokeDelegate();

private void Invoke_Click(object sender, EventArgs e)
{
   myTextBox.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
public void InvokeMethod()
{
   myTextBox.Text = "Executed the given delegate";
}

Remarks

The delegate is called asynchronously, and this method returns immediately. You can call this method from any thread, even the thread that owns the control's handle. If the control's handle does not exist yet, this method searches up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, BeginInvoke will throw an exception. Exceptions within the delegate method are considered untrapped and will be sent to the application's untrapped exception handler.

You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. EndInvoke will block until the return value can be retrieved.

Note

Most methods on a control can only be called from the thread where the control was created. In addition to the InvokeRequired property, there are four methods on a control that are thread safe: Invoke, BeginInvoke, EndInvoke, and CreateGraphics if the handle for the control has already been created. Calling 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 invoke methods always invoke their callbacks on the control's thread.

Note

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

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9