码迷,mamicode.com
首页 > Windows程序 > 详细

C# WinForm 异步执行耗时操作并将过程显示在界面中

时间:2018-04-11 10:37:15      阅读:829      评论:0      收藏:0      [点我收藏+]

标签:end   r++   tar   ati   info   private   stat   main   event   

 private void button3_Click(object sender, EventArgs e)
        {
            RunAsync(() =>
            {
                // Just loop.
                int ctr = 0;
                for (ctr = 0; ctr <= 10; ctr++)
                {
                    Thread.Sleep(2000);
                    RunInMainthread(() =>
                    {
                        textBox1.Text = ctr.ToString();
                    });
                }
              
            });
           
            MessageBox.Show("");
        }  // 异步线程
        public static void RunAsync(Action action)
        {
            ((Action)(delegate()
            {
                action.Invoke();
            })).BeginInvoke(null, null);
        }
        public void RunInMainthread(Action action)
        {
            this.BeginInvoke((Action)(delegate()
            {
                action.Invoke();
            }));
        }

C# WinForm 异步执行耗时操作并将过程显示在界面中

标签:end   r++   tar   ati   info   private   stat   main   event   

原文地址:https://www.cnblogs.com/computer-lzy/p/8793685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!