1. 程式人生 > >跨執行緒使用使用者介面的四種方法(BackgroundThreadUpdateUI)

跨執行緒使用使用者介面的四種方法(BackgroundThreadUpdateUI)

namespace BackgroundThreadUpdateUI
{
    // For Method Two
    public delegate void SetTextCallback(string text);

    public partial class MainForm : Form
    {
        // For Method One
        System.Threading.SynchronizationContext m_SyncContext = null;

        // For Method Three
        private BackgroundWorker m_Worker = null;

        public MainForm()
        {
            InitializeComponent();

            // For Method One
            this.m_SyncContext = System.Threading.SynchronizationContext.Current;

            // For Method Three
            this.m_Worker = new System.ComponentModel.BackgroundWorker();
            this.m_Worker.WorkerReportsProgress = true;
            this.m_Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            this.m_Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

            // For Method Four
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        #region For Method One

        private void btnStartThreadForMethodOne_Click(object sender, EventArgs e)
        {
            System.Threading.Thread th = new System.Threading.Thread(ThreadRunForMethodOne);
            th.IsBackground = true;
            th.Start();
        }

        private void ThreadRunForMethodOne()
        {
            m_SyncContext.Post(Post, "SynchronizationContext-Post.");
        }

        private void Post(object text)
        {
            this.txtDisplayForMethodOne.Text = text.ToString();
        }

        #endregion

        #region For Method Two

        private void btnStartThreadForMethodTwo_Click(object sender, EventArgs e)
        {
            System.Threading.Thread th = new System.Threading.Thread(ThreadRunForMethodTwo);
            th.IsBackground = true;
            th.Start();
        }

        private void ThreadRunForMethodTwo()
        {
            this.SetText("This text was set safely.");
        }

        private void SetText(string text)
        {
            if (this.txtDisplayForMethodTwo.InvokeRequired)
            {
                while (!this.txtDisplayForMethodOne.IsHandleCreated)
                {
                    if (this.txtDisplayForMethodTwo.Disposing || this.txtDisplayForMethodOne.IsDisposed)
                        return;
                }
                this.txtDisplayForMethodTwo.Invoke(new SetTextCallback(SetText), new object[] { text });
            }
            else
            {
                this.txtDisplayForMethodTwo.Text = text;
            }
        }

        #endregion

        #region For Method Three

        private void btnStartThreadForMethodThree_Click(object sender, EventArgs e)
        {
            if (!this.m_Worker.IsBusy)
            {
                this.m_Worker.RunWorkerAsync();
            }
        }

        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            txtDisplayForMethodThree.Text = e.Result.ToString();
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = "Update UI by using BackgroundWorker";
        }
        #endregion

        #region For Method Four

        private void btnStartThreadForMethodFour_Click(object sender, EventArgs e)
        {
            System.Threading.Thread th = new System.Threading.Thread(ThreadRunForMethodFour);
            th.IsBackground = true;
            th.Start();
        }

        private void ThreadRunForMethodFour()
        {
            txtDisplayForMethodFour.Text = "CheckForIllegalCrossThreadCalls = false";
        }

        #endregion
        
    }
}

附件下載連結: