Control.BeginInvoke Method (Delegate, Object())
Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Public Function BeginInvoke ( method As Delegate, ParamArray args As Object() ) As IAsyncResult
A delegate to a method that takes parameters of the same number and type that are contained in the args parameter.
An array of objects to pass as arguments to the given method. This can be null if no arguments are needed.
Return ValueType: System.IAsyncResult
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.
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.
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 themethod.
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
Available since 1.1