線程間操作無效: 從不是創建控件“XXX”的線程訪問它。

今天遇到這個問題,百度了下,把解決的方法總結出來。
我們在ui線程創建的子線程操作ui控件時,系統提示錯誤詳細信息爲:
線程間操作無效: 從不是創建控件“XXX”的線程訪問它。

 

就我知道的有三種方法,先看一下msdn的介紹:

訪問 Windows 窗體控件本質上不是線程安全的。如果有兩個或多個線程操作某一控件的狀態,則可能會迫使該控件進入一種不一致的狀態。還可能出現其他與線程相關的 bug,包括爭用情況和死鎖。確保以線程安全方式訪問控件非常重要。

 

看下解決辦法吧

1、把CheckForIllegalCrossThreadCalls設置爲false

2、利用委託

複製代碼
delegate void SetTextCallBack(string text);
        
private void SetText(string text)
        {
            
if (this.txt_a.InvokeRequired)
            {
                SetTextCallBack stcb 
= new SetTextCallBack(SetText);
                
this.Invoke(stcb , new object[] { text});
            }
            
else
            {
                
this.txt_a.Text = text;
            }
        }
 
        
private void LoadData()
        {
            SetText(
"測試");
        }
 
 
        
//窗體加載時,用線程加載數據
        private void Frm_ImportManager_Load(object sender, EventArgs e)
        {
            ThreadStart ts 
= new ThreadStart(LoadData);
            Thread thread 
= new Thread(ts);
            thread.Name 
= "LoadData";
            thread.Start();
        }
複製代碼

 


3、使用 BackgroundWorker控件

在應用程序中實現多線程的首選方式是使用 BackgroundWorker 組件。BackgroundWorker 組件使用事件驅動模型實現多線程。輔助線程運行 DoWork 事件處理程序,創建控件的線程運行 ProgressChanged 和 RunWorkerCompleted 事件處理程序。注意不要從 DoWork 事件處理程序調用您的任何控件。

下面的代碼示例不異步執行任何工作,因此沒有 DoWork 事件處理程序的實現。TextBox控件的 Text 屬性在 RunWorkerCompleted 事件處理程序中直接設置。

 

複製代碼
// This event handler starts the form's 
        
// BackgroundWorker by calling RunWorkerAsync.
        
//
        
// The Text property of the TextBox control is set
        
// when the BackgroundWorker raises the RunWorkerCompleted
        
// event.
        private void setTextBackgroundWorkerBtn_Click(
            
object sender, 
            EventArgs e)
        {
            
this.backgroundWorker1.RunWorkerAsync();
        }
        
        
// This event handler sets the Text property of the TextBox
        
// control. It is called on the thread that created the 
        
// TextBox control, so the call is thread-safe.
        
//
        
// BackgroundWorker is the preferred way to perform asynchronous
        
// operations.

        
private void backgroundWorker1_RunWorkerCompleted(
            
object sender, 
            RunWorkerCompletedEventArgs e)
        {
            
this.textBox1.Text = 
                
"This text was set safely by BackgroundWorker.";
        }

複製代碼

 

 

大家可以參考下MSDN:
如何:對 Windows 窗體控件進行線程安全調用

http://msdn.microsoft.com/zh-cn/visualc/ms171728(VS.85,printer).aspx

發佈了124 篇原創文章 · 獲贊 16 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章