Control.BeginInvoke 方法

定義

在建立控制項基礎控制代碼的執行緒上執行非同步委派。Executes a delegate asynchronously on the thread that the control's underlying handle was created on.

多載

BeginInvoke(Delegate)

在建立控制項基礎控制代碼的執行緒上執行指定的非同步委派。Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

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)

在建立控制項基礎控制代碼的執行緒上執行指定的非同步委派。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

沒有參數的方法委派。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. 委派方法內的例外狀況會視為 untrapped,並會傳送至應用程式的 untrapped 例外狀況處理常式。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[])

在建立控制項基礎控制代碼的執行緒上,以指定的引數非同步執行指定的委派。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

方法的委派,採用和 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. 委派方法內的例外狀況會視為 untrapped,並會傳送至應用程式的 untrapped 例外狀況處理常式。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.

另請參閱

適用於