随笔 - 49  文章 - 0  评论 - 6  阅读 - 10万

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;
        }
    }
View Code
复制代码

 

posted on   朱迎春  阅读(396)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示