欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-05 23:12:02
...

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

源码如下:

// 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
        
    }

 

partial class MainForm
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.btnStartThreadForMethodOne = new System.Windows.Forms.Button();
            this.txtDisplayForMethodOne = new System.Windows.Forms.TextBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label2 = new System.Windows.Forms.Label();
            this.btnStartThreadForMethodTwo = new System.Windows.Forms.Button();
            this.txtDisplayForMethodTwo = new System.Windows.Forms.TextBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.label3 = new System.Windows.Forms.Label();
            this.btnStartThreadForMethodThree = new System.Windows.Forms.Button();
            this.txtDisplayForMethodThree = new System.Windows.Forms.TextBox();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.label4 = new System.Windows.Forms.Label();
            this.btnStartThreadForMethodFour = new System.Windows.Forms.Button();
            this.txtDisplayForMethodFour = new System.Windows.Forms.TextBox();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btnStartThreadForMethodOne);
            this.groupBox1.Controls.Add(this.txtDisplayForMethodOne);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(368, 132);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Method One";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(33, 29);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(173, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "Using SynchronizationContext";
            // 
            // btnStartThreadForMethodOne
            // 
            this.btnStartThreadForMethodOne.Location = new System.Drawing.Point(34, 80);
            this.btnStartThreadForMethodOne.Name = "btnStartThreadForMethodOne";
            this.btnStartThreadForMethodOne.Size = new System.Drawing.Size(304, 35);
            this.btnStartThreadForMethodOne.TabIndex = 1;
            this.btnStartThreadForMethodOne.Text = "启动线程";
            this.btnStartThreadForMethodOne.UseVisualStyleBackColor = true;
            this.btnStartThreadForMethodOne.Click += new System.EventHandler(this.btnStartThreadForMethodOne_Click);
            // 
            // txtDisplayForMethodOne
            // 
            this.txtDisplayForMethodOne.Location = new System.Drawing.Point(34, 53);
            this.txtDisplayForMethodOne.Name = "txtDisplayForMethodOne";
            this.txtDisplayForMethodOne.Size = new System.Drawing.Size(304, 21);
            this.txtDisplayForMethodOne.TabIndex = 0;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.btnStartThreadForMethodTwo);
            this.groupBox2.Controls.Add(this.txtDisplayForMethodTwo);
            this.groupBox2.Location = new System.Drawing.Point(386, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(368, 132);
            this.groupBox2.TabIndex = 0;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Method Two";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(33, 29);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(155, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "Using Invoke/BegainInvoke";
            // 
            // btnStartThreadForMethodTwo
            // 
            this.btnStartThreadForMethodTwo.Location = new System.Drawing.Point(34, 80);
            this.btnStartThreadForMethodTwo.Name = "btnStartThreadForMethodTwo";
            this.btnStartThreadForMethodTwo.Size = new System.Drawing.Size(304, 35);
            this.btnStartThreadForMethodTwo.TabIndex = 1;
            this.btnStartThreadForMethodTwo.Text = "启动线程";
            this.btnStartThreadForMethodTwo.UseVisualStyleBackColor = true;
            this.btnStartThreadForMethodTwo.Click += new System.EventHandler(this.btnStartThreadForMethodTwo_Click);
            // 
            // txtDisplayForMethodTwo
            // 
            this.txtDisplayForMethodTwo.Location = new System.Drawing.Point(34, 53);
            this.txtDisplayForMethodTwo.Name = "txtDisplayForMethodTwo";
            this.txtDisplayForMethodTwo.Size = new System.Drawing.Size(304, 21);
            this.txtDisplayForMethodTwo.TabIndex = 0;
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.label3);
            this.groupBox3.Controls.Add(this.btnStartThreadForMethodThree);
            this.groupBox3.Controls.Add(this.txtDisplayForMethodThree);
            this.groupBox3.Location = new System.Drawing.Point(12, 166);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(368, 132);
            this.groupBox3.TabIndex = 0;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Method Three";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(33, 29);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(137, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "Using BackgroundWorker";
            // 
            // btnStartThreadForMethodThree
            // 
            this.btnStartThreadForMethodThree.Location = new System.Drawing.Point(34, 80);
            this.btnStartThreadForMethodThree.Name = "btnStartThreadForMethodThree";
            this.btnStartThreadForMethodThree.Size = new System.Drawing.Size(304, 35);
            this.btnStartThreadForMethodThree.TabIndex = 1;
            this.btnStartThreadForMethodThree.Text = "启动线程";
            this.btnStartThreadForMethodThree.UseVisualStyleBackColor = true;
            this.btnStartThreadForMethodThree.Click += new System.EventHandler(this.btnStartThreadForMethodThree_Click);
            // 
            // txtDisplayForMethodThree
            // 
            this.txtDisplayForMethodThree.Location = new System.Drawing.Point(34, 53);
            this.txtDisplayForMethodThree.Name = "txtDisplayForMethodThree";
            this.txtDisplayForMethodThree.Size = new System.Drawing.Size(304, 21);
            this.txtDisplayForMethodThree.TabIndex = 0;
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.label4);
            this.groupBox4.Controls.Add(this.btnStartThreadForMethodFour);
            this.groupBox4.Controls.Add(this.txtDisplayForMethodFour);
            this.groupBox4.Location = new System.Drawing.Point(386, 166);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(368, 132);
            this.groupBox4.TabIndex = 0;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Method Four";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(33, 29);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(173, 12);
            this.label4.TabIndex = 2;
            this.label4.Text = "Using Cancel Thread Crossing";
            // 
            // btnStartThreadForMethodFour
            // 
            this.btnStartThreadForMethodFour.Location = new System.Drawing.Point(34, 80);
            this.btnStartThreadForMethodFour.Name = "btnStartThreadForMethodFour";
            this.btnStartThreadForMethodFour.Size = new System.Drawing.Size(304, 35);
            this.btnStartThreadForMethodFour.TabIndex = 1;
            this.btnStartThreadForMethodFour.Text = "启动线程";
            this.btnStartThreadForMethodFour.UseVisualStyleBackColor = true;
            this.btnStartThreadForMethodFour.Click += new System.EventHandler(this.btnStartThreadForMethodFour_Click);
            // 
            // txtDisplayForMethodFour
            // 
            this.txtDisplayForMethodFour.Location = new System.Drawing.Point(34, 53);
            this.txtDisplayForMethodFour.Name = "txtDisplayForMethodFour";
            this.txtDisplayForMethodFour.Size = new System.Drawing.Size(304, 21);
            this.txtDisplayForMethodFour.TabIndex = 0;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(766, 316);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Four Motheds For Update UI In Background Thread";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnStartThreadForMethodOne;
        private System.Windows.Forms.TextBox txtDisplayForMethodOne;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnStartThreadForMethodTwo;
        private System.Windows.Forms.TextBox txtDisplayForMethodTwo;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button btnStartThreadForMethodThree;
        private System.Windows.Forms.TextBox txtDisplayForMethodThree;
        private System.Windows.Forms.GroupBox groupBox4;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnStartThreadForMethodFour;
        private System.Windows.Forms.TextBox txtDisplayForMethodFour;
    }