.NET Framework Class Library
Control..::.BeginInvoke Method (Delegate, array<Object>[]()[])

Updated: October 2008

Executes the specified delegate asynchronously with the specified arguments, 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)
Syntax

Visual Basic (Declaration)
Public Function BeginInvoke ( _
    method As Delegate, _
    ParamArray args As Object() _
) As IAsyncResult
Visual Basic (Usage)
Dim instance As Control
Dim method As [Delegate]
Dim args As Object()
Dim returnValue As IAsyncResult

returnValue = instance.BeginInvoke(method, _
    args)
C#
public IAsyncResult BeginInvoke(
    Delegate method,
    params Object[] args
)
Visual C++
public:
virtual IAsyncResult^ BeginInvoke(
    Delegate^ method, 
    ... array<Object^>^ args
) sealed
JScript
public final function BeginInvoke(
    method : Delegate, 
    ... args : Object[]
) : IAsyncResult

Parameters

method
Type: System..::.Delegate
A delegate to a method that takes parameters of the same number and type that are contained in the args parameter.
args
Type: array<System..::.Object>[]()[]
An array of objects to pass as arguments to the given method. This can be nullNothingnullptra null reference (Nothing in Visual Basic) if no arguments are needed.

Return Value

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

Implements

ISynchronizeInvoke..::.BeginInvoke(Delegate, array<Object>[]()[])
Exceptions

ExceptionCondition
InvalidOperationException

No appropriate window handle can be found.

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.

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.

Examples

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

Visual Basic
Delegate Sub MyDelegate(myControl As Label, myArg2 As String)

Private Sub Button_Click(sender As Object, e As EventArgs)
   Dim myArray(1) As Object

   myArray(0) = New Label()
   myArray(1) = "Enter a Value"
   myTextBox.BeginInvoke(New MyDelegate(AddressOf DelegateMethod), myArray)
End Sub 'Button_Click

Public Sub DelegateMethod(myControl As Label, myCaption As String)
   myControl.Location = New Point(16, 16)
   myControl.Size = New Size(80, 25)
   myControl.Text = myCaption
   Me.Controls.Add(myControl)
End Sub 'DelegateMethod

C#
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);
}
Visual C++
private:
   delegate void MyDelegate(
   Label^ myControl, String^ myArg2 );
   void Button_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      array<Object^>^myArray = gcnew array<Object^>(2);
      myArray[ 0 ] = gcnew Label;
      myArray[ 1 ] = "Enter a Value";
      myTextBox->BeginInvoke( gcnew MyDelegate( this, &MyForm::DelegateMethod ), myArray );
   }

   void DelegateMethod( Label^ myControl, String^ myCaption )
   {
      myControl->Location = Point(16,16);
      myControl->Size = System::Drawing::Size( 80, 25 );
      myControl->Text = myCaption;
      this->Controls->Add( myControl );
   }

   delegate void InvokeDelegate();
Platforms

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.
Version Information

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0
See Also

Reference

Other Resources

Change History

Date

History

Reason

October 2008

Added exception information.

Information enhancement.

Tags :


Page view tracker