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. 所有其他的方法呼叫中,您應該使用其中一種叫用方法來對控制項的執行緒的呼叫封送處理。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
立即呼叫端呼叫 unmanaged 程式碼。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. 所有其他的方法呼叫中,您應該使用其中一種叫用方法來對控制項的執行緒的呼叫封送處理。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.

另請參閱

適用於