C# invoke和begininvoke的一点随笔

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(TripleKill));

            th.Start();
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "5");
            MessageBox.Show("hello world");
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "hello world");
        }


        private void TripleKill()
        {
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "TripleKill-1");
            //MessageBox.Show("hello456");
            this.Invoke(new DelegateTest(changeText1));
            this.BeginInvoke(new DelegateTest(changeBtnText));
            this.BeginInvoke(new DelegateTest(DoubleKill)).ToString();
        }


        private void changeText1()
        {
            //MessageBox.Show("BeginInvoke-changeText1");
            textBox1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "789";
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "changeText1.2");
        }


        private void changeBtnText()
        {
            Thread.Sleep(4000);
            this.button1.Text = "click me";
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "changeBtnText-click me-3");
            MessageBox.Show("click me");
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "changeBtnText-click me-31");
            Thread.Sleep(2000);
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "changeBtnText-click me-32");
        }


        private void DoubleKill()
        {
            Thread.Sleep(3000);
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "TripleKill-Invoke-4");
            MessageBox.Show("Invoke");
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + "TripleKill-Invoke-41");
        }

结果:

2024-08-28 14:07:19.502--5
2024-08-28 14:07:19.553--changeText1.2
2024-08-28 14:07:23.560--changeBtnText-click me-3
2024-08-28 14:07:26.579--TripleKill-Invoke-4
2024-08-28 14:07:27.976--TripleKill-Invoke-41
2024-08-28 14:07:28.952--changeBtnText-click me-31
2024-08-28 14:07:30.952--changeBtnText-click me-32
2024-08-28 14:07:57.866--hello world

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值