Control.BeginInvoke Control.BeginInvoke Control.BeginInvoke Control.BeginInvoke Method

定义

在创建控件的基础句柄所在线程上异步执行委托。Executes a delegate asynchronously on the thread that the control's underlying handle was created on.

重载

BeginInvoke(Delegate) BeginInvoke(Delegate) BeginInvoke(Delegate) BeginInvoke(Delegate)

在创建控件的基础句柄所在线程上异步执行指定委托。Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

BeginInvoke(Delegate, Object[]) BeginInvoke(Delegate, Object[]) BeginInvoke(Delegate, Object[]) 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(Delegate) BeginInvoke(Delegate) BeginInvoke(Delegate) BeginInvoke(Delegate)

在创建控件的基础句柄所在线程上异步执行指定委托。Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

public:
 IAsyncResult ^ BeginInvoke(Delegate ^ method);
public IAsyncResult BeginInvoke (Delegate method);
member this.BeginInvoke : Delegate -> IAsyncResult
Public Function BeginInvoke (method As Delegate) As IAsyncResult

参数

method
Delegate Delegate Delegate Delegate

对不带参数的方法的委托。A delegate to a method that takes no parameters.

返回

一个表示 IAsyncResult 操作的结果的 BeginInvoke(Delegate)An IAsyncResult that represents the result of the BeginInvoke(Delegate) operation.

异常

找不到适当的窗口句柄。No appropriate window handle can be found.

示例

下面的代码示例演示BeginInvoke方法的用法。The following code example demonstrates a use of the BeginInvoke method.

private:
   void Invoke_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      myTextBox->BeginInvoke( gcnew InvokeDelegate( this, &MyForm::InvokeMethod ) );
   }

   void InvokeMethod()
   {
      myTextBox->Text = "Executed the given delegate";
   }
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";
}
Delegate Sub InvokeDelegate()

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

Public Sub InvokeMethod()
   myTextBox.Text = "Executed the given delegate"
End Sub

注解

委托是异步调用的,并且此方法会立即返回。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. 如果找不到合适的句柄BeginInvoke ,将引发异常。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.

如果必要, EndInvoke可以调用来检索委托的返回值,但这不是必需的。You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. EndInvoke在可以检索返回值之前将一直阻止。EndInvoke will block until the return value can be retrieved.

备注

控件的大多数方法只能从创建控件的线程调用。Most methods on a control can only be called from the thread where the control was created. 除了InvokeRequired属性外,控件上有四种线程安全方法: InvokeBeginInvokeEndInvokeCreateGraphics (如果已创建控件的句柄)。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. CreateGraphics后台线程上创建控件句柄之前调用可能导致非法的跨线程调用。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.

安全性

SecurityPermission
供直接调用方调用非托管代码。for the immediate caller to call unmanaged code. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode

另请参阅

BeginInvoke(Delegate, Object[]) BeginInvoke(Delegate, Object[]) BeginInvoke(Delegate, Object[]) BeginInvoke(Delegate, Object[])

在创建控件的基础句柄所在线程上,用指定的参数异步执行指定委托。Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on.

public:
 virtual IAsyncResult ^ BeginInvoke(Delegate ^ method, cli::array <System::Object ^> ^ args);
public IAsyncResult BeginInvoke (Delegate method, object[] args);
abstract member BeginInvoke : Delegate * obj[] -> IAsyncResult
override this.BeginInvoke : Delegate * obj[] -> IAsyncResult
Public Function BeginInvoke (method As Delegate, args As Object()) As IAsyncResult

参数

method
Delegate Delegate Delegate Delegate

一个方法委托,它采用的参数的数量和类型与 args 参数中所包含的相同。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. 如果不需要自变量,则可以为 nullThis can be null if no arguments are needed.

返回

一个表示 IAsyncResult 操作的结果的 BeginInvoke(Delegate)An IAsyncResult that represents the result of the BeginInvoke(Delegate) operation.

实现

异常

找不到适当的窗口句柄。No appropriate window handle can be found.

示例

下面的代码示例演示BeginInvoke方法的用法。The following code example demonstrates a use of the BeginInvoke method.

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();
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);
}
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

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

注解

委托是异步调用的,并且此方法会立即返回。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. 如果找不到合适的句柄BeginInvoke ,将引发异常。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.

如果必要, EndInvoke可以调用来检索委托的返回值,但这不是必需的。You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. EndInvoke在可以检索返回值之前将一直阻止。EndInvoke will block until the return value can be retrieved.

备注

控件的大多数方法只能从创建控件的线程调用。Most methods on a control can only be called from the thread where the control was created. 除了InvokeRequired属性外,控件上有四种线程安全方法: InvokeBeginInvokeEndInvokeCreateGraphics (如果已创建控件的句柄)。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. CreateGraphics后台线程上创建控件句柄之前调用可能导致非法的跨线程调用。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.

另请参阅

适用于