2010-04-08 17 views
0

我試圖從一個boost ::線程訪問wxDialog成員:「未處理的異常」 錯誤混合的boost ::線程時與wxWidgets的GUI

void AnotherThread(myWxDialog *dlg) 
{ 
    wxMessageBox(dlg->TextBox1->GetValue(), "It works!"); // This throws an error 
} 

void myWxDialog::OnButtonClick(wxCommandEvent &event) 
{ 
    boost::thread myThread(AnotherThread, this); 
} 

而且我得到了這個錯誤:

Unhandled exception at 0x004043d7 in MyProgram.exe: 0xC0000005: Access violation reading location 0xbaadf00d.

我覺得這種行爲在不同線程之間是不允許的。

那麼,有沒有其他的方式來做同樣的事情?

任何形式的幫助,將不勝感激。 :)

(微軟的Visual C++ 2008年速成版)

+0

沒有GUI是線程安全的。當您嘗試時,wxWidgets會讓您的應用崩潰。 – 2010-04-08 16:17:41

回答

2

0xbaadf00d表明您正在訪問一個未初始化的指針;如果我是你,我會深入挖掘調試器,看看究竟是哪個指針是(在TextBox1中的dlg?中GetValue()返回的內容?wxMessageBox中的其他位置?)。這可以幫助你瞭解問題所在。

不過,最大的錯誤是試圖從另一個線程訪問GUI的東西:如明確規定here

When writing a multi-threaded application, it is strongly recommended that no secondary threads call GUI functions. The design which uses one GUI thread and several worker threads which communicate with the main one using events is much more robust and will undoubtedly save you countless problems (example: under Win32 a thread can only access GDI objects such as pens, brushes, device contexts created by itself and not by the other threads).

在那裏,你還可以找到有關如何解決事件和其他的wxWidgets這些限制的一些建議設備。

+0

抱歉,我太急於發佈此問題。主要問題是由於在GUI一側訪問一個處理對象的函數而引起的。但是,對於其他遇到類似問題的人,您的答案可能會有用。謝謝。 – 2010-04-08 16:35:32