共用方式為


Control.Invoke 方法

定義

在擁有控件基礎視窗句柄的線程上執行委派。

多載

Invoke(Action)

在擁有控件基礎視窗句柄的線程上執行指定的委派。

Invoke(Delegate)

在擁有控件基礎視窗句柄的線程上執行指定的委派。

Invoke(Delegate, Object[])

在擁有控件基礎視窗句柄的線程上,使用指定的自變數清單執行指定的委派。

Invoke<T>(Func<T>)

在擁有控件基礎視窗句柄的線程上執行指定的委派。

Invoke(Action)

在擁有控件基礎視窗句柄的線程上執行指定的委派。

public:
 void Invoke(Action ^ method);
public void Invoke (Action method);
member this.Invoke : Action -> unit
Public Sub Invoke (method As Action)

參數

method
Action

委派,其中包含控件線程內容中要呼叫的方法。

適用於

Invoke(Delegate)

在擁有控件基礎視窗句柄的線程上執行指定的委派。

public:
 System::Object ^ Invoke(Delegate ^ method);
public object Invoke (Delegate method);
member this.Invoke : Delegate -> obj
Public Function Invoke (method As Delegate) As Object

參數

method
Delegate

委派,其中包含控件線程內容中要呼叫的方法。

傳回

從叫用的委派傳回值,如果委派沒有傳回值,則 null

範例

下列程式代碼範例顯示包含委派的控制件。 委派會封裝方法,將專案新增至清單框,而且這個方法會在擁有窗體基礎句柄的線程上執行。 當使用者按鍵時,Invoke 執行委派。

/*
The following example demonstrates the 'Invoke(Delegate*)' method of 'Control class.
A 'ListBox' and a 'Button' control are added to a form, containing a delegate
which encapsulates a method that adds items to the listbox. This function is executed
on the thread that owns the underlying handle of the form. When user clicks on button
the above delegate is executed using 'Invoke' method.
*/

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Threading;

public ref class MyFormControl: public Form
{
public:
   delegate void AddListItem();
   AddListItem^ myDelegate;

private:
   Button^ myButton;
   Thread^ myThread;
   ListBox^ myListBox;

public:
   MyFormControl();
   void AddListItemMethod()
   {
      String^ myItem;
      for ( int i = 1; i < 6; i++ )
      {
         myItem = "MyListItem {0}",i;
         myListBox->Items->Add( myItem );
         myListBox->Update();
         Thread::Sleep( 300 );
      }
   }

private:
   void Button_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      myThread = gcnew Thread( gcnew ThreadStart( this, &MyFormControl::ThreadFunction ) );
      myThread->Start();
   }

   void ThreadFunction();
};


// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// containing a delegate which encapsulates a method that adds items to the listbox.
public ref class MyThreadClass
{
private:
   MyFormControl^ myFormControl1;

public:
   MyThreadClass( MyFormControl^ myForm )
   {
      myFormControl1 = myForm;
   }

   void Run()
   {
      // Execute the specified delegate on the thread that owns
      // 'myFormControl1' control's underlying window handle.
      myFormControl1->Invoke( myFormControl1->myDelegate );
   }
};


MyFormControl::MyFormControl()
{
   myButton = gcnew Button;
   myListBox = gcnew ListBox;
   myButton->Location = Point( 72, 160 );
   myButton->Size = System::Drawing::Size( 152, 32 );
   myButton->TabIndex = 1;
   myButton->Text = "Add items in list box";
   myButton->Click += gcnew EventHandler( this, &MyFormControl::Button_Click );
   myListBox->Location = Point( 48, 32 );
   myListBox->Name = "myListBox";
   myListBox->Size = System::Drawing::Size( 200, 95 );
   myListBox->TabIndex = 2;
   ClientSize = System::Drawing::Size( 292, 273 );
   array<Control^>^ temp0 = {myListBox,myButton};
   Controls->AddRange( temp0 );
   Text = " 'Control_Invoke' example";
   myDelegate = gcnew AddListItem( this, &MyFormControl::AddListItemMethod );
}

void MyFormControl::ThreadFunction()
{
   MyThreadClass^ myThreadClassObject = gcnew MyThreadClass( this );
   myThreadClassObject->Run();
}

int main()
{
   MyFormControl^ myForm = gcnew MyFormControl;
   myForm->ShowDialog();
}
/*
The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.
A 'ListBox' and a 'Button' control are added to a form, containing a delegate
which encapsulates a method that adds items to the listbox. This function is executed
on the thread that owns the underlying handle of the form. When user clicks on button
the above delegate is executed using 'Invoke' method.


*/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

   public class MyFormControl : Form
   {
      public delegate void AddListItem();
      public AddListItem myDelegate;
      private Button myButton;
      private Thread myThread;
      private ListBox myListBox;
      public MyFormControl()
      {
         myButton = new Button();
         myListBox = new ListBox();
         myButton.Location = new Point(72, 160);
         myButton.Size = new Size(152, 32);
         myButton.TabIndex = 1;
         myButton.Text = "Add items in list box";
         myButton.Click += new EventHandler(Button_Click);
         myListBox.Location = new Point(48, 32);
         myListBox.Name = "myListBox";
         myListBox.Size = new Size(200, 95);
         myListBox.TabIndex = 2;
         ClientSize = new Size(292, 273);
         Controls.AddRange(new Control[] {myListBox,myButton});
         Text = " 'Control_Invoke' example";
         myDelegate = new AddListItem(AddListItemMethod);
      }
      static void Main()
      {
         MyFormControl myForm = new MyFormControl();
         myForm.ShowDialog();
      }
      public void AddListItemMethod()
      {
         String myItem;
         for(int i=1;i<6;i++)
         {
            myItem = "MyListItem" + i.ToString();
            myListBox.Items.Add(myItem);
            myListBox.Update();
            Thread.Sleep(300);
         }
      }
      private void Button_Click(object sender, EventArgs e)
      {
         myThread = new Thread(new ThreadStart(ThreadFunction));
         myThread.Start();
      }
      private void ThreadFunction()
      {
         MyThreadClass myThreadClassObject  = new MyThreadClass(this);
         myThreadClassObject.Run();
      }
   }

// The following code assumes a 'ListBox' and a 'Button' control are added to a form, 
// containing a delegate which encapsulates a method that adds items to the listbox.

   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }

      public void Run()
      {
         // Execute the specified delegate on the thread that owns
         // 'myFormControl1' control's underlying window handle.
         myFormControl1.Invoke(myFormControl1.myDelegate);
      }
   }
' The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.
' A 'ListBox' and a 'Button' control are added to a form, containing a delegate
' which encapsulates a method that adds items to the listbox. This function is executed
' on the thread that owns the underlying handle of the form. When user clicks on button
' the above delegate is executed using 'Invoke' method.

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class MyFormControl
   Inherits Form

   Delegate Sub AddListItem()
   Public myDelegate As AddListItem
   Private myButton As Button
   Private myThread As Thread
   Private myListBox As ListBox

   Public Sub New()
      myButton = New Button()
      myListBox = New ListBox()
      myButton.Location = New Point(72, 160)
      myButton.Size = New Size(152, 32)
      myButton.TabIndex = 1
      myButton.Text = "Add items in list box"
      AddHandler myButton.Click, AddressOf Button_Click
      myListBox.Location = New Point(48, 32)
      myListBox.Name = "myListBox"
      myListBox.Size = New Size(200, 95)
      myListBox.TabIndex = 2
      ClientSize = New Size(292, 273)
      Controls.AddRange(New Control() {myListBox, myButton})
      Text = " 'Control_Invoke' example"
      myDelegate = New AddListItem(AddressOf AddListItemMethod)
   End Sub

   Shared Sub Main()
      Dim myForm As New MyFormControl()
      myForm.ShowDialog()
   End Sub

   Public Sub AddListItemMethod()
      Dim myItem As String
      Dim i As Integer
      For i = 1 To 5
         myItem = "MyListItem" + i.ToString()
         myListBox.Items.Add(myItem)
         myListBox.Update()
         Thread.Sleep(300)
      Next i
   End Sub

   Private Sub Button_Click(sender As Object, e As EventArgs)
      myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
      myThread.Start()
   End Sub

   Private Sub ThreadFunction()
      Dim myThreadClassObject As New MyThreadClass(Me)
      myThreadClassObject.Run()
   End Sub
End Class


' The following code assumes a 'ListBox' and a 'Button' control are added to a form, 
' containing a delegate which encapsulates a method that adds items to the listbox.
Public Class MyThreadClass
   Private myFormControl1 As MyFormControl

   Public Sub New(myForm As MyFormControl)
      myFormControl1 = myForm
   End Sub

   Public Sub Run()
      ' Execute the specified delegate on the thread that owns
      ' 'myFormControl1' control's underlying window handle.
      myFormControl1.Invoke(myFormControl1.myDelegate)
   End Sub

End Class

備註

委派類似於 C 或 C++ 語言中的函式指標。 委派會封裝委派物件內方法的參考。 然後,委派物件可以傳遞至呼叫參考方法的程序代碼,而且在編譯時期叫用的方法可能未知。 不同於 C 或 C++ 中的函式指標,委派是面向物件、類型安全且更安全。

Invoke 方法會搜尋控件的父鏈結,直到它找到控件或窗體,如果目前控件的基礎視窗句柄還不存在,該控件具有視窗句柄。 如果找不到適當的句柄,Invoke 方法會擲回例外狀況。 呼叫期間引發的例外狀況會傳播回呼叫端。

注意

除了 InvokeRequired 屬性之外,還有四個線程安全控件的方法:InvokeBeginInvokeEndInvoke,以及如果已經建立控件的句柄,CreateGraphics。 在背景線程上建立控件的句柄之前呼叫 CreateGraphics 可能會導致不合法的跨線程呼叫。 對於所有其他方法呼叫,您應該使用其中一個叫用方法封送處理控件線程的呼叫。

委派可以是 EventHandler的實例,在此情況下,sender 參數將包含此控件,而事件參數將包含 EventArgs.Empty。 委派也可以是 MethodInvoker實例,或任何其他接受 void 參數列表的委派。 對 EventHandlerMethodInvoker 委派的呼叫會比呼叫另一種類型的委派更快。

注意

如果處理訊息的線程不再作用中,可能會擲回例外狀況。

另請參閱

適用於

Invoke(Delegate, Object[])

在擁有控件基礎視窗句柄的線程上,使用指定的自變數清單執行指定的委派。

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

參數

method
Delegate

方法的委派,採用包含在 args 參數中的相同數位和型別的參數。

args
Object[]

要當做自變數傳遞至指定方法的物件陣列。 如果方法沒有自變數,則可以 null 此參數。

傳回

Object,其中包含所叫用之委派的傳回值,如果委派沒有傳回值,則為 null

實作

範例

下列程式代碼範例顯示包含委派的控制件。 委派會封裝將專案加入清單框的方法,而且這個方法會在擁有窗體基礎句柄的線程上執行,並使用指定的自變數。 當使用者按鍵時,Invoke 執行委派。

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Threading;
ref class MyFormControl: public Form
{
public:
   delegate void AddListItem( String^ myString );
   AddListItem^ myDelegate;

private:
   Button^ myButton;
   Thread^ myThread;
   ListBox^ myListBox;

public:
   MyFormControl();
   void AddListItemMethod( String^ myString );

private:
   void Button_Click( Object^ sender, EventArgs^ e );
   void ThreadFunction();
};

ref class MyThreadClass
{
private:
   MyFormControl^ myFormControl1;

public:
   MyThreadClass( MyFormControl^ myForm )
   {
      myFormControl1 = myForm;
   }

   String^ myString;
   void Run()
   {
      for ( int i = 1; i <= 5; i++ )
      {
         myString = String::Concat( "Step number ", i, " executed" );
         Thread::Sleep( 400 );
         
         // Execute the specified delegate on the thread that owns
         // 'myFormControl1' control's underlying window handle with
         // the specified list of arguments.
         array<Object^>^myStringArray = {myString};
         myFormControl1->Invoke( myFormControl1->myDelegate, myStringArray );

      }
   }

};

MyFormControl::MyFormControl()
{
   myButton = gcnew Button;
   myListBox = gcnew ListBox;
   myButton->Location = Point(72,160);
   myButton->Size = System::Drawing::Size( 152, 32 );
   myButton->TabIndex = 1;
   myButton->Text = "Add items in list box";
   myButton->Click += gcnew EventHandler( this, &MyFormControl::Button_Click );
   myListBox->Location = Point(48,32);
   myListBox->Name = "myListBox";
   myListBox->Size = System::Drawing::Size( 200, 95 );
   myListBox->TabIndex = 2;
   ClientSize = System::Drawing::Size( 292, 273 );
   array<Control^>^formControls = {myListBox,myButton};
   Controls->AddRange( formControls );
   Text = " 'Control_Invoke' example ";
   myDelegate = gcnew AddListItem( this, &MyFormControl::AddListItemMethod );
}

void MyFormControl::AddListItemMethod( String^ myString )
{
   myListBox->Items->Add( myString );
}

void MyFormControl::Button_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   myThread = gcnew Thread( gcnew ThreadStart( this, &MyFormControl::ThreadFunction ) );
   myThread->Start();
}

void MyFormControl::ThreadFunction()
{
   MyThreadClass^ myThreadClassObject = gcnew MyThreadClass( this );
   myThreadClassObject->Run();
}

int main()
{
   MyFormControl^ myForm = gcnew MyFormControl;
   myForm->ShowDialog();
}
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

   public class MyFormControl : Form
   {
      public delegate void AddListItem(String myString);
      public AddListItem myDelegate;
      private Button myButton;
      private Thread myThread;
      private ListBox myListBox;
      public MyFormControl()
      {
         myButton = new Button();
         myListBox = new ListBox();
         myButton.Location = new Point(72, 160);
         myButton.Size = new Size(152, 32);
         myButton.TabIndex = 1;
         myButton.Text = "Add items in list box";
         myButton.Click += new EventHandler(Button_Click);
         myListBox.Location = new Point(48, 32);
         myListBox.Name = "myListBox";
         myListBox.Size = new Size(200, 95);
         myListBox.TabIndex = 2;
         ClientSize = new Size(292, 273);
         Controls.AddRange(new Control[] {myListBox,myButton});
         Text = " 'Control_Invoke' example ";
         myDelegate = new AddListItem(AddListItemMethod);
      }
      static void Main()
      {
         MyFormControl myForm = new MyFormControl();
         myForm.ShowDialog();
      }
      public void AddListItemMethod(String myString)
      {
            myListBox.Items.Add(myString);
      }
      private void Button_Click(object sender, EventArgs e)
      {
         myThread = new Thread(new ThreadStart(ThreadFunction));
         myThread.Start();
      }
      private void ThreadFunction()
      {
         MyThreadClass myThreadClassObject  = new MyThreadClass(this);
         myThreadClassObject.Run();
      }
   }
   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }
      String myString;

      public void Run()
      {

         for (int i = 1; i <= 5; i++)
         {
            myString = "Step number " + i.ToString() + " executed";
            Thread.Sleep(400);
            // Execute the specified delegate on the thread that owns
            // 'myFormControl1' control's underlying window handle with
            // the specified list of arguments.
            myFormControl1.Invoke(myFormControl1.myDelegate,
                                   new Object[] {myString});
         }
      }
   }
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class MyFormControl
   Inherits Form

   Delegate Sub AddListItem(myString As String)
   Public myDelegate As AddListItem
   Private myButton As Button
   Private myThread As Thread
   Private myListBox As ListBox

   Public Sub New()
      myButton = New Button()
      myListBox = New ListBox()
      myButton.Location = New Point(72, 160)
      myButton.Size = New Size(152, 32)
      myButton.TabIndex = 1
      myButton.Text = "Add items in list box"
      AddHandler myButton.Click, AddressOf Button_Click
      myListBox.Location = New Point(48, 32)
      myListBox.Name = "myListBox"
      myListBox.Size = New Size(200, 95)
      myListBox.TabIndex = 2
      ClientSize = New Size(292, 273)
      Controls.AddRange(New Control() {myListBox, myButton})
      Text = " 'Control_Invoke' example "
      myDelegate = New AddListItem(AddressOf AddListItemMethod)
   End Sub

   Shared Sub Main()
      Dim myForm As New MyFormControl()
      myForm.ShowDialog()
   End Sub

   Public Sub AddListItemMethod(myString As String)
      myListBox.Items.Add(myString)
   End Sub

   Private Sub Button_Click(sender As Object, e As EventArgs)
      myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
      myThread.Start()
   End Sub

   Private Sub ThreadFunction()
      Dim myThreadClassObject As New MyThreadClass(Me)
      myThreadClassObject.Run()
   End Sub
End Class

Public Class MyThreadClass
   Private myFormControl1 As MyFormControl

   Public Sub New(myForm As MyFormControl)
      myFormControl1 = myForm
   End Sub
   Private myString As String

   Public Sub Run()

      Dim i As Integer
      For i = 1 To 5
         myString = "Step number " + i.ToString() + " executed"
         Thread.Sleep(400)
         ' Execute the specified delegate on the thread that owns
         ' 'myFormControl1' control's underlying window handle with
         ' the specified list of arguments.
         myFormControl1.Invoke(myFormControl1.myDelegate, New Object() {myString})
      Next i

   End Sub
End Class

備註

委派類似於 C 或 C++ 語言中的函式指標。 委派會封裝委派物件內方法的參考。 然後,委派物件可以傳遞至呼叫參考方法的程序代碼,而且在編譯時期叫用的方法可能未知。 不同於 C 或 C++ 中的函式指標,委派是面向物件、類型安全且更安全。

如果控件的句柄還不存在,這個方法會搜尋控件的父鏈結,直到找到具有視窗句柄的控件或窗體為止。 如果找不到適當的句柄,這個方法會擲回例外狀況。 呼叫期間引發的例外狀況會傳播回呼叫端。

注意

除了 InvokeRequired 屬性之外,還有四個線程安全控件的方法:InvokeBeginInvokeEndInvoke,以及如果已經建立控件的句柄,CreateGraphics。 在背景線程上建立控件的句柄之前呼叫 CreateGraphics 可能會導致不合法的跨線程呼叫。 對於所有其他方法呼叫,您應該使用其中一個叫用方法封送處理控件線程的呼叫。

委派可以是 EventHandler的實例,在此情況下,參數將取決於 args 值:

  • 如果未傳遞任何參數,傳送者參數會包含此控制件,而事件參數將包含 EventArgs.Empty
  • 傳遞單一參數時,sender 參數會包含第一個args元素,而事件參數會包含 EventArgs.Empty
  • 如果傳遞多個參數,sender 參數會包含來自 args的第一個專案,而 EventArgs 參數會包含第二個元素。

EventHandlerMethodInvoker 委派的呼叫會比呼叫另一種類型的委派更快。

注意

如果處理訊息的線程不再作用中,可能會擲回例外狀況。

另請參閱

適用於

Invoke<T>(Func<T>)

在擁有控件基礎視窗句柄的線程上執行指定的委派。

public:
generic <typename T>
 T Invoke(Func<T> ^ method);
public T Invoke<T> (Func<T> method);
member this.Invoke : Func<'T> -> 'T
Public Function Invoke(Of T) (method As Func(Of T)) As T

類型參數

T

method的傳回型別。

參數

method
Func<T>

在控件線程內容中呼叫的函式。

傳回

T

所叫用之函式的傳回值。

適用於