C#中 支线程Invoke和BeginInvoke用法和区别

当支线程调用winfrom中的控件:

第一种办法:禁止编译器对跨线程访问做检查:

        public Form1()
        {
            InitializeComponent();
            // 加入这行
            Control.CheckForIllegalCrossThreadCalls = false;
        }

第二种办法: 使用delegate和invoke或者beginInvoke来从其他线程中调用控件

首先建一个winfrom页面如图:

Invoke用法程序:

        /// <summary>
        /// 支线线程调用Invoke
        /// 输出--begin--
        ///     --end--
        ///     Before!Invoke!Main
        ///总结:Invoke的委托在主线程中执行
        ///Invoke在支线程中调用也会阻塞主线程。(当点击button1后,我试图去点击button2,却发现程序被阻塞了)
        ///Invoke还会阻塞支线程。(因为输出结果中没有出现After)      
        /// </summary>
        private void ThreadInvoke()
        {
            listBox1.Items.Add("--begin--");
            new Thread(() =>
            {
                Thread.CurrentThread.Name = "ThreadInvoke";
                string temp = "Before!";
                listBox1.Invoke(new Action(() =>
                {
                    Thread.Sleep(10000);
                    this.listBox1.Items.Add(temp += "Invoke!" + Thread.CurrentThread.Name);
                }));
                temp += "After!";
            }).Start();
            listBox1.Items.Add("--end--");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.Name = "Main";
            ThreadInvoke();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("button2_Click");
        }

 

 

BeginInvoke用法程序:
        /// <summary>
        /// 支线程调用BeginInvoke
        /// 输出--begin--
        ///     --end--
        ///     Before!After!Invoke!Main
        /// 总结:BeginInvoke在主线程中执行。
        ///       BeginInvoke在支线程中调用也会阻塞主线程。
        ///       BeginInvoke相对于支线程是异步的。 
        /// </summary>
        private void ThreadBeginInvoke()
        {
            listBox1.Items.Add("--begin--");
            new Thread(() =>
            {
                Thread.CurrentThread.Name = "ThreadBeginInvoke";
                string temp = "Before!";
                listBox1.BeginInvoke(new Action(() =>
                {
                    Thread.Sleep(10000);
                    this.listBox1.Items.Add(temp += "Invoke!" + Thread.CurrentThread.Name);
                }));
                temp += "After!";
            }).Start();
            listBox1.Items.Add("--end--");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.Name = "Main";
            ThreadBeginInvoke();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("button2_Click");
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@榴莲酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值