您的位置:首页 > 编程语言 > C#

C# 窗体程序后台线程操作窗体控件

2017-11-01 10:03 351 查看
这里提供四种方式,见代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

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

        

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#