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 'Invoke_Click

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

注解

以异步方式调用该委托,此方法将立即返回。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属性,在控件上都是线程安全的四种方法: InvokeBeginInvokeEndInvoke,和CreateGraphics如果已创建控件的句柄。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. 对于所有其他方法调用,您应使用 invoke 方法之一来封送对控件的线程的调用。For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread. Invoke 方法始终调用控件的线程上其回调。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, params object[] args);
abstract member BeginInvoke : Delegate * obj[] -> IAsyncResult
override this.BeginInvoke : Delegate * obj[] -> IAsyncResult
Public Function BeginInvoke (method As Delegate, ParamArray 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 '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

注解

以异步方式调用该委托,此方法将立即返回。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属性,在控件上都是线程安全的四种方法: InvokeBeginInvokeEndInvoke,和CreateGraphics如果已创建控件的句柄。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. 对于所有其他方法调用,您应使用 invoke 方法之一来封送对控件的线程的调用。For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread. Invoke 方法始终调用控件的线程上其回调。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.

另请参阅

适用于