BeginInvoke Method (Delegate)
.NET Framework Class Library
Control..::.BeginInvoke Method (Delegate)

Updated: October 2008

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Function BeginInvoke ( _
    method As Delegate _
) As IAsyncResult
Visual Basic (Usage)
Dim instance As Control
Dim method As [Delegate]
Dim returnValue As IAsyncResult

returnValue = instance.BeginInvoke(method)
C#
public IAsyncResult BeginInvoke(
    Delegate method
)
Visual C++
public:
IAsyncResult^ BeginInvoke(
    Delegate^ method
)
JScript
public function BeginInvoke(
    method : Delegate
) : IAsyncResult

Parameters

method
Type: System..::.Delegate
A delegate to a method that takes no parameters.

Return Value

Type: System..::.IAsyncResult
An IAsyncResult that represents the result of the BeginInvoke operation.
ExceptionCondition
InvalidOperationException

No appropriate window handle can be found.

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.

NoteNote:

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.

NoteNote:

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

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

Visual Basic
Delegate Sub InvokeDelegate()

Private Sub Invoke_Click(sender As Object, e As EventArgs)
   myTextBox.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod))
End Sub 'Invoke_Click

Public Sub InvokeMethod()
   myTextBox.Text = "Executed the given delegate"
End Sub 'InvokeMethod
C#
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";
}
Visual C++
private:
   void Invoke_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      myTextBox->BeginInvoke( gcnew InvokeDelegate( this, &MyForm::InvokeMethod ) );
   }

   void InvokeMethod()
   {
      myTextBox->Text = "Executed the given delegate";
   }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

Date

History

Reason

October 2008

Added exception information.

Information enhancement.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Exception if control is disposed      Daniel Stutzbach   |   Edit   |   Show History
If the control is disposed, BeginInvoke will throw an InvalidOperationException and claim "Invoke or BeginInvoke cannot be called on a control until the window handle has been created", which is misleading. In fact the handle has already been created... and destroyed.
Tags What's this?: Add a tag
Flag as ContentBug
MUST call EndInvoke to avoid leaks      cheeso   |   Edit   |   Show History
The MS documentation is inconsistent on this point, but it is apparently true that code MUST call EndInvoke() after BeginInvoke() to avoid memory leaks.

This page says "You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required". On the other hand, the more general page on Async Programming: http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx ... says that you must ALWAYS call EndInvoke: "Important Note: No matter which technique you use, always call EndInvoke to complete your asynchronous call."

Flag as ContentBug
Processing
Page view tracker