打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Control.Invoke 方法 (System.Windows.Forms) | Microsoft Learn

Control.Invoke 方法

  • 参考

定义

命名空间:
System.Windows.Forms
程序集:
System.Windows.Forms.dll

在拥有此控件的基础窗口句柄的线程上执行委托。

重载

Invoke(Action)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。

Invoke(Delegate)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。

Invoke(Delegate, Object[])

在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。

Invoke<T>(Func<T>)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。

Invoke(Action)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。

C#
public void Invoke (Action method);

参数

method
Action

包含要在控件的线程上下文中调用的方法的委托。

适用于

Windows Desktop 9 和其他版本
产品 版本
Windows Desktop 6, 7, 8, 9

Invoke(Delegate)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。

C#
public object Invoke (Delegate method);

参数

method
Delegate

包含要在控件的线程上下文中调用的方法的委托。

返回

正在被调用的委托的返回值,或者如果委托没有返回值,则为 null

示例

下面的代码示例演示包含委托的控件。 委托封装向列表框添加项的方法,此方法在拥有窗体基础句柄的线程上执行。 当用户单击按钮时, Invoke 运行委托。

C#
/*
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);
      }
   }

注解

委托类似于 C 或 C++ 语言中的函数指针。 委托在委托对象中封装对方法的引用。 然后,可以将委托对象传递给调用引用方法的代码,并且要调用的方法在编译时可能未知。 与 C 或 C++ 中的函数指针不同,委托面向对象、类型安全且更安全。

如果当前控件的基础窗口句柄尚不存在,则 Invoke 该方法将搜索控件的父链,直到找到具有窗口句柄的控件或窗体。 如果找不到适当的句柄,方法 Invoke 将引发异常。 调用期间引发的异常将传播回调用方。

备注

除了 InvokeRequired 属性外,控件上还有四种线程安全的方法: InvokeBeginInvokeEndInvokeCreateGraphics (如果已创建控件的句柄)。 在后台线程上创建控件句柄之前调用 CreateGraphics 可能会导致非法跨线程调用。 对于所有其他方法调用,应使用调用方法之一来封送对控件线程的调用。

委托可以是 的 EventHandler实例,在这种情况下,发送方参数将包含此控件,事件参数将包含 EventArgs.Empty。 委托也可以是 的 MethodInvoker实例,也可以是采用 void 参数列表的任何其他委托。 调用 EventHandlerMethodInvoker 委托比调用另一种类型的委托要快。

备注

如果应处理消息的线程不再处于活动状态,可能会引发异常。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Invoke(Delegate, Object[])

在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。

C#
public object Invoke (Delegate method, params object?[]? args);

参数

method
Delegate

一个方法委托,它采用的参数的数量和类型与 args 参数中所包含的相同。

args
Object[]

作为指定方法的参数传递的对象数组。 如果此方法没有参数,该参数可以是 null

返回

Object,它包含正被调用的委托返回值;如果该委托没有返回值,则为 null

实现

示例

下面的代码示例演示包含委托的控件。 委托封装一个方法,该方法将项添加到列表框,此方法使用指定的参数在拥有窗体基础句柄的线程上执行。 当用户单击按钮时, Invoke 运行委托。

C#
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});
         }
      }
   }

注解

委托类似于 C 或 C++ 语言中的函数指针。 委托在委托对象中封装对方法的引用。 然后,可以将委托对象传递给调用引用方法的代码,并且要调用的方法在编译时可能未知。 与 C 或 C++ 中的函数指针不同,委托面向对象、类型安全且更安全。

如果控件的句柄尚不存在,此方法将搜索控件的父链,直到找到具有窗口句柄的控件或窗体。 如果没有找到适当的句柄,此方法将引发异常。 调用期间引发的异常将传播回调用方。

备注

除了 InvokeRequired 属性外,控件上还有四种线程安全的方法: InvokeBeginInvokeEndInvokeCreateGraphics (如果已创建控件的句柄)。 在后台线程上创建控件句柄之前调用 CreateGraphics 可能会导致非法跨线程调用。 对于所有其他方法调用,应使用调用方法之一来封送对控件线程的调用。

委托可以是 的 EventHandler实例,在这种情况下,发送方参数将包含此控件,事件参数将包含 EventArgs.Empty。 委托也可以是 的 MethodInvoker实例,也可以是采用 void 参数列表的任何其他委托。 调用 EventHandlerMethodInvoker 委托比调用另一种类型的委托要快。

备注

如果应处理消息的线程不再处于活动状态,可能会引发异常。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Invoke<T>(Func<T>)

在拥有此控件的基础窗口句柄的线程上执行指定的委托。

C#
public T Invoke<T> (Func<T> method);

类型参数

T

method返回类型。

参数

method
Func<T>

在控件的线程上下文中调用的函数。

返回

T

正在调用的函数的返回值。

适用于

Windows Desktop 9 和其他版本
产品 版本
Windows Desktop 6, 7, 8, 9
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Control.Invoke与消息队列
【分析】浅谈C#中Control的Invoke与BeginInvoke在主副线程中的执行顺序和区别(SamWang)
谈.Net委托与线程——解决窗体假死 - 横竖都溢 - 博客园
对 Windows 窗体控件进行线程安全调用 - 夜淡茶清.shenfx - 博客园
Invoke and BeginInvoke
C#委托和多线程[转载]
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服