最簡單的方法(不推薦):
在窗體構造函數中寫Control.CheckForIllegalCrossThreadCalls =false;
為什么不推薦上面的方法:
為避免空間造成死鎖, .net framework 2.0 之后 拒絕多線程訪問空間, 以前Control.CheckForIllegalCrossThreadCalls 默認就是flase, 2.0后默認為true。當然如果能保證程序中不會好幾個線程同時操作一個控件用上面的方法也可以, 如果存在多個線程一起操作控件使用下面的方法; 下面是來自微軟官網給出的示例:
三種處理方案見源碼:
Form1.cs源碼:
using System; using System.ComponentModel; using System.Threading; using System.Windows.Forms; namespace CrossThreadDemo { public partial class Form1 : Form { //設置這個委托支持異步調用文本框控件的文本屬性。 delegate void SetTextCallback(string text); //這個線程用於演示和線程安全的和不安全的方法來調用一個Windows窗體控件。 private Thread demoThread = null; //這個BackgroundWorker用於演示執行異步操作, 推薦使用該方法。 private BackgroundWorker backgroundWorker1; private TextBox textBox1; private Button setTextUnsafeBtn; private Button setTextSafeBtn; private Button setTextBackgroundWorkerBtn; public Form1() { InitializeComponent(); } //這個事件處理程序創建一個線程,該線程演示用不安全的方式調用 Windows窗體, 在.net framework2.0后被禁用 private void setTextUnsafeBtn_Click(object sender,EventArgs e) { this.demoThread = new Thread(new ThreadStart(this.ThreadProcUnsafe)); this.demoThread.Start(); } //這個方法執行工作線程, 文本框控件不安全的調用, 一執行就會報錯. private void ThreadProcUnsafe() { //線程間操作無效: 從不是創建控件“textBox1”的線程訪問它。 this.textBox1.Text = "This text was set unsafely."; } //這個事件處理程序創建一個線程,該線程使用安全的方式調用 Windows窗體. private void setTextSafeBtn_Click(object sender,EventArgs e) { this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe)); this.demoThread.Start(); } //這個方法執行工作線程,使 文本框控件的線程安全的調用. private void ThreadProcSafe() { this.SetText("This text was set safely."); } //這個方法演示了線程通過安全的模式, 調用一個Windows窗體控件。如果調用線程和當前創建了文本框控件的線程不用,該方法創建一個SetTextCallback,並使用了異步調用自身調用方法,
//如果調用線程創建的線程是一樣的文本框控件,直接設置文本屬性。 private void SetText(string text) { if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { //在此設置textBox1的文本 this.textBox1.Text = text; } } //這個事件處理程序通過調用RunWorkerAsync BackgroundWorker。設置文本框控件的文本屬性, 用BackgroundWorker提高RunWorkerCompleted事件。 private void setTextBackgroundWorkerBtn_Click(object sender,EventArgs e) { this.backgroundWorker1.RunWorkerAsync(); } //這個事件處理程序設置文本框的文本屬性控制。它調用創建的線程文本框控件,所以調用是線程安全的,異步執行BackgroundWorker是首選方法操作. private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e) { this.textBox1.Text ="This text was set safely by BackgroundWorker."; } } }
Program.cs源碼:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new CrossThreadDemo.Form1()); } } }
Form1.Designer.cs源碼:
namespace CrossThreadDemo { partial class Form1 { /// <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.textBox1 = new System.Windows.Forms.TextBox(); this.setTextUnsafeBtn = new System.Windows.Forms.Button(); this.setTextSafeBtn = new System.Windows.Forms.Button(); this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button(); this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(240, 21); this.textBox1.TabIndex = 0; // // setTextUnsafeBtn // this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55); this.setTextUnsafeBtn.Name = "setTextUnsafeBtn"; this.setTextUnsafeBtn.Size = new System.Drawing.Size(75, 23); this.setTextUnsafeBtn.TabIndex = 1; this.setTextUnsafeBtn.Text = "Unsafe Call"; this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click); // // setTextSafeBtn // this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55); this.setTextSafeBtn.Name = "setTextSafeBtn"; this.setTextSafeBtn.Size = new System.Drawing.Size(75, 23); this.setTextSafeBtn.TabIndex = 2; this.setTextSafeBtn.Text = "Safe Call"; this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click); // // setTextBackgroundWorkerBtn // this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55); this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn"; this.setTextBackgroundWorkerBtn.Size = new System.Drawing.Size(75, 23); this.setTextBackgroundWorkerBtn.TabIndex = 3; this.setTextBackgroundWorkerBtn.Text = "Safe BW Call"; this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click); // // backgroundWorker1 // this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); // // Form1 // this.ClientSize = new System.Drawing.Size(268, 96); this.Controls.Add(this.setTextBackgroundWorkerBtn); this.Controls.Add(this.setTextSafeBtn); this.Controls.Add(this.setTextUnsafeBtn); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion } }
窗體界面截圖: