COMPUTER_LZY

要输就输给追求,要嫁就嫁给幸福

导航

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

 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();
            }));
        }

posted on 2018-04-11 09:52  CANYOUNG  阅读(1244)  评论(0编辑  收藏  举报