System.Windows.Forms.Control.Invoke与BeginInvoke

WinForm的UI对象只能在UI线程中操作,在非UI线程中操作UI对象,会引发不可预知的错误,这时就需要用到Control.Invoke或者Control.BeginInvoke。

用户线程调用Control.BeginInvoke会向UI消息队列发送一个带委托消息,Control.BeginInvoke不会阻塞用户线程,直接返回IAsyncResult对象。

用户线程调用Control.EndInvoke(IAsyncResult),Control.EndInvoke会阻塞用户线程,直到委托执行完成,并返回委托的返回值。没有返回值返回null。

Control.Invoke相当于Control.BeginInvoke和Control.EndInvoke的合体,会阻塞用户线程,直到委托执行完成,并返回委托的返回值。没有返回值返回null。

根据类继承关系,在窗口中可以直接使用BeginInvoke、EndInvoke、Invoke。

System.Object
??System.MarshalByRefObject
????System.ComponentModel.Component
??????System.Windows.Forms.Control
????????System.Windows.Forms.ScrollableControl
??????????System.Windows.Forms.ContainerControl
????????????System.Windows.Forms.Form

实验示例:

窗体:

代码:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 在非UI线程中操作UI对象,调试运行会报错。
            // 直接运行会使程序置于不可预知风险之中。
            new Thread(() =>
            {
                progressBar1.Value = 100;
            }).Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new Thread(() => {
                var result = this.Invoke(new Func<int, int, string>((n1, n2) =>
                {
                    for(int i = 0; i <= 100; i += 10)
                    {
                        progressBar1.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1+n2).ToString();
                }), 100, 21);
                MessageBox.Show(result.GetType().ToString() + ":" + result);
            }).Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                IAsyncResult asyncResult = this.BeginInvoke(new Func<int, int, string>((n1, n2) =>
                {
                    for (int i = 0; i <= 100; i += 10)
                    {
                        progressBar1.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1 + n2).ToString();
                }), 200, 32);
                MessageBox.Show("BeginInvoke不会阻塞");
                var result = this.EndInvoke(asyncResult);
                MessageBox.Show("EndInvoke会阻塞," + result.GetType().ToString() + ":" + result);
            }).Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 连续给两个委托,由于UI线程只有一个,两个委托只能先后执行
            new Thread(() =>
            {
                IAsyncResult asyncResult1 = this.BeginInvoke(new Func<int, int, string>((n1, n2) =>
                {
                    for (int i = 0; i <= 100; i += 10)
                    {
                        progressBar1.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1 + n2).ToString();
                }), 200, 32);
                IAsyncResult asyncResult2 = this.BeginInvoke(new Func<int, int, string>((n1, n2) =>
                {
                    for (int i = 0; i <= 100; i += 10)
                    {
                        progressBar2.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1 + n2).ToString();
                }), 400, 64);
                MessageBox.Show("BeginInvoke不会阻塞");
                var result1 = this.EndInvoke(asyncResult1);
                MessageBox.Show("EndInvoke(asyncResult1)返回");
                var result2 = this.EndInvoke(asyncResult2);
                MessageBox.Show("EndInvoke(asyncResult2)返回");
                MessageBox.Show(
                    result1.GetType().ToString() + ":" + result1 + "\r\n" +
                    result2.GetType().ToString() + ":" + result2);
            }).Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // 要等精度条更新完成后,点击才能响应
            MessageBox.Show("ha");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            progressBar2.Value = 0;
        }
    }

原文地址:https://www.cnblogs.com/zhuyingchun/p/9592351.html

时间: 2024-08-14 03:20:24

System.Windows.Forms.Control.Invoke与BeginInvoke的相关文章

System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....

#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll #endregion using System.Collections; using System.ComponentModel; using Syst

“FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。

一旦运行就显示:“FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”.如果是有意隐藏,请使用关键字 new. 定位错误是在:this.Text="FormMain"; 后来才知道是调试模式的问题,我当时是把调试模式切换到 release 现在切换回 debug就没有问题了.

System.Windows.Forms.ListView : Control

#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll #endregion using System.Collections; using System.ComponentModel; using Syst

System.Windows.Forms

1 File: winforms\Managed\System\WinForms\DataGridView.cs 2 Project: ndp\fx\src\System.Windows.Forms.csproj (System.Windows.Forms) 3 4 //------------------------------------------------------------------------------ 5 // <copyright file="DataGridVi

System.Diagnostic.Process.Start vs System.Windows.Forms.Help.ShowHelp 打开CHM文件

CHM文件,Microsoft Compiled HTML Help,即"已编辑的帮助文件",包含一系列的HTML文件,index文件和其它的导航工具,经常作为产品的帮助文件[1]. 在.Net程序中,打开这种文件最简单的方式就是调用System.Windows.Forms.Help.ShowHelp()方法.根据MSDN,重载了四种调用方式[2].Control为父控件,string为Help文件的URL,HelpNavigator是一个枚举类型,可以采用Index或者Topic或者

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

一.System.Windows.Forms.Timer 1.基于Windows消息循环,用事件方式触发,在界面线程执行:是使用得比较多的Timer,Timer Start之后定时(按设定的Interval)调用挂接在Tick事件上的EvnetHandler.在这种Timer的EventHandler中可 以直接获取和修改UI元素而不会出现问题--因为这种Timer实际上就是在UI线程自身上进行调用的. 2.它是一个基于Form的计时器3.创建之后,你可以使用Interval设置Tick之间的跨

错误 128 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn”

原因是DataGridView中列的Name属性值和DataPropertyName属性值一样,比如Name="CardID",DataPropertyName="CardID",这样会出现 :错误 128 无法将类型"string"隐式转换为"System.Windows.Forms.DataGridViewTextBoxColumn"

WPF 添加ACTIVEX 控件 ——System.Windows.Forms.AxHost+InvalidActiveXStateException异常

---恢复内容开始--- 一.ACTIVEX及其相关概念 COM(Component Object Model,对象组件模型): DCOM(Distributed COM,分布式对象组件模型): CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构): 动态数据交换(Dynamic Data Exchange,DDE),解决了数据的更新,但数据格式仍是问题: 对象的链接与嵌入(Object Linking and Embedde

System.Windows.Forms.Timer反编译学习

1 using System; 2 using System.ComponentModel; 3 using System.Globalization; 4 using System.Runtime; 5 using System.Runtime.InteropServices; 6 using System.Threading; 7 namespace System.Windows.Forms 8 { 9 [DefaultEvent("Tick"), DefaultProperty(