Control.Invoke Control.Invoke Control.Invoke Control.Invoke Method

定义

在拥有此控件的基础窗口句柄的线程上执行委托。Executes a delegate on the thread that owns the control's underlying window handle.

重载

Invoke(Delegate) Invoke(Delegate) Invoke(Delegate) Invoke(Delegate)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。Executes the specified delegate on the thread that owns the control's underlying window handle.

Invoke(Delegate, Object[]) Invoke(Delegate, Object[]) Invoke(Delegate, Object[]) Invoke(Delegate, Object[])

在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.

Invoke(Delegate) Invoke(Delegate) Invoke(Delegate) Invoke(Delegate)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。Executes the specified delegate on the thread that owns the control's underlying window handle.

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 Delegate Delegate Delegate

包含要在控件的线程上下文中调用的方法的委托。A delegate that contains a method to be called in the control's thread context.

返回

正在被调用的委托的返回值,或者如果委托没有返回值,则为 nullThe return value from the delegate being invoked, or null if the delegate has no return value.

示例

下面的代码示例显示了包含一个委托的控件。The following code example shows controls that contain a delegate. 委托封装的方法将项添加到列表框中,并拥有的窗体的基础句柄的线程上执行此方法。The delegate encapsulates a method that adds items to the list box, and this method is executed on the thread that owns the underlying handle of the form. 当用户单击按钮,Invoke运行该委托。When the user clicks on the button, Invoke runs the delegate.

/*
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
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 'New

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

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

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

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


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

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

End Class 'MyThreadClass

注解

委托是类似于在 C 中的函数指针或C++语言。Delegates are similar to function pointers in C or C++ languages. 委托封装到委托对象中的方法的引用。Delegates encapsulate a reference to a method inside a delegate object. 委托对象然后传递到调用所引用的方法的代码,并且要调用的方法可以在编译时未知。The delegate object can then be passed to code that calls the referenced method, and the method to be invoked can be unknown at compile time. 与 C 中的函数指针或C++中,委托是面向对象的、 类型安全且更安全。Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and more secure.

Invoke方法搜索控件的父链,直到它找到的控件或窗体具有一个窗口处理如果尚不存在当前控件的基础窗口句柄。The Invoke method searches up the control's parent chain until it finds a control or form that has a window handle if the current control's underlying window handle does not exist yet. 如果找不到任何合适的句柄,Invoke方法将引发异常。If no appropriate handle can be found, the Invoke method will throw an exception. 在调用期间引发的异常将传播回调用方。Exceptions that are raised during the call will be propagated back to the caller.

备注

除了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.

委托可以是实例EventHandler,在这种情况下发件人参数将包含此控件和事件参数将包含EventArgs.EmptyThe delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. 委托还可以是实例MethodInvoker,或任何其他 void 参数列表的委托。The delegate can also be an instance of MethodInvoker, or any other delegate that takes a void parameter list. 调用EventHandlerMethodInvoker委托会比其他类型的委托调用更快。A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate.

备注

如果应处理该消息的线程不再处于活动状态,可能会引发异常。An exception might be thrown if the thread that should process the message is no longer active.

另请参阅

Invoke(Delegate, Object[]) Invoke(Delegate, Object[]) Invoke(Delegate, Object[]) Invoke(Delegate, Object[])

在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.

public:
 virtual System::Object ^ Invoke(Delegate ^ method, ... cli::array <System::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, ParamArray args As Object()) As Object

参数

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 specified method. 如果此方法没有参数,该参数可以是 nullThis parameter can be null if the method takes no arguments.

返回

Object,它包含正被调用的委托返回值;如果该委托没有返回值,则为 nullAn Object that contains the return value from the delegate being invoked, or null if the delegate has no return value.

实现

示例

下面的代码示例显示了包含一个委托的控件。The following code example shows controls that contain a delegate. 委托封装的方法将项添加到列表框中,并拥有的窗体中,使用指定的参数的基础句柄的线程上执行此方法。The delegate encapsulates a method that adds items to the list box, and this method is executed on the thread that owns the underlying handle of the form, using the specified arguments. 当用户单击按钮,Invoke运行该委托。When the user clicks on the button, Invoke runs the delegate.

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

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

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

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

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

Public Class MyThreadClass
   Private myFormControl1 As MyFormControl

   Public Sub New(myForm As MyFormControl)
      myFormControl1 = myForm
   End Sub 'New
   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 'Run
End Class 'MyThreadClass

注解

委托是类似于在 C 中的函数指针或C++语言。Delegates are similar to function pointers in C or C++ languages. 委托封装到委托对象中的方法的引用。Delegates encapsulate a reference to a method inside a delegate object. 委托对象然后传递到调用所引用的方法的代码,并且要调用的方法可以在编译时未知。The delegate object can then be passed to code that calls the referenced method, and the method to be invoked can be unknown at compile time. 与 C 中的函数指针或C++中,委托是面向对象的、 类型安全且更安全。Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and more secure.

如果控件的句柄尚不存在,此方法搜索控件的父链,直到它找到的控件或窗体具有一个窗口句柄。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. 如果找不到任何合适的句柄,此方法将引发异常。If no appropriate handle can be found, this method throws an exception. 在调用期间引发的异常将传播回调用方。Exceptions that are raised during the call will be propagated back to the caller.

备注

除了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.

委托可以是实例EventHandler,在这种情况下发件人参数将包含此控件和事件参数将包含EventArgs.EmptyThe delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. 委托还可以是实例MethodInvoker,或任何其他 void 参数列表的委托。The delegate can also be an instance of MethodInvoker, or any other delegate that takes a void parameter list. 调用EventHandlerMethodInvoker委托会比其他类型的委托调用更快。A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate.

备注

如果应处理该消息的线程不再处于活动状态,可能会引发异常。An exception might be thrown if the thread that should process the message is no longer active.

另请参阅

适用于