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

相關文章
相關標籤/搜索