线程间值的传递

问题

在C# 的应用程序开发中,我们经常要把UI线程和工作线程分开,防止界面停止响应.同时我们又需要在工作线程中更新UI界面上的控件
下面介绍几种常用的方法

准备工作

打开VS,新建一个名为 ThreadDemo 的窗体应用程序,放一个button和lable控件,点击button会启动一个线程来更新Label的值


阅读目录
线程间操作无效
第一种办法:禁止编译器对跨线程访问做检查
第二种办法: 使用delegate和invoke来从其他线程中调用控件
第三种办法: 使用delegate和BeginInvoke来从其他线程中控制控件
第四种办法: 使用BackgroundWorker组件

线程间操作无效

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Windows.Forms;
 7 
 8 namespace ThreadDemo
 9 {
10     public partial class Form1: Form
11     {
12         public Form1()
13         {
14             InitializeComponent();
15         }       
16         private void btn_start_Click(object sender, EventArgs e)
17         {
18             Thread thread = new Thread(new ParameterizedThreadStart(UpdateLabel));
19             thread.Start("更新了Label");
20         }
21 
22         private void UpdateLabel(object str)
23         {
24             this.label1.Text = str.ToString();
25         }
26     }
27 }

运行后, 程序会报错 "跨线程操作无效,从不是创建"label1"的线程访问它"或者"线程间操作无效;从不是创建控件“lable1”的线程访问它。",这是因为.NET禁止了跨线程调用控件, 否则谁都可以操作控件,最后可能造成错误,如图:

下面介绍几种跨线程调用控件的方法

第一种办法:禁止编译器对跨线程访问做检查

这是最简单的办法, 相当于不检查线程之间的冲突,允许各个线程随便乱搞,最后Lable1控件的值是什么就难以预料了 (不推荐使用这种方法)

只要把这句话

Control.CheckForIllegalCrossThreadCalls = false;

放在窗体的构造函数里的 InitializeComponent(); 后面就行了

第二种办法: 使用delegate和invoke来从其他线程中调用控件

调用控件的invoke方法,就可以控制控件了,例如

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ThreadDemo
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(new ParameterizedThreadStart(UpdateLabel2));
            thread1.Start("更新Label");
        }
private void UpdateLabel2(object str) { if (label2.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action<string> actionDelegate = (x) => { this.label2.Text = x.ToString(); }; // 或者 // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; }; this.label2.Invoke(actionDelegate, str); } else { this.label2.Text = str.ToString(); } } } }

第三种办法: 使用delegate和BeginInvoke来从其他线程中控制控件
只要把上面的 this.label2.Invoke(actionDelegate, str); 中的 Invoke 改为BeginInvoke方法就可以了

Invoke方法和BeginInvoke方法的区别是

Invoke方法是同步的, 它会等待工作线程完成,
BeginInvoke方法是异步的, 它会另起一个线程去完成工作线程

第四种办法: 使用BackgroundWorker组件(推荐使用这个方法)
BackgroundWorker是.NET里面用来执行多线程任务的控件,它允许编程者在一个单独的线程上执行一些操作。耗时的操作(如下载和数据库事务)。用法简单

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ThreadDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerAsync("Tank");
            }
        }
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            // 这里是后台线程, 是在另一个线程上完成的
            // 这里是真正做事的工作线程
            // 可以在这里做一些费时的,复杂的操作
            Thread.Sleep(5000);
            e.Result = e.Argument + "工作线程完成";
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //这时后台线程已经完成,并返回了主线程,所以可以直接使用UI控件了 
            this.label4.Text = e.Result.ToString();
        }
    }
}
原文地址:https://www.cnblogs.com/abc1069/p/4065062.html