我在嘗試爲我的wxWidgets GUI應用程序創建一個工作線程時會遇到問題(基本上,我很困惑),它將修改其中一個GUI屬性本身。 (在這種情況下,wxTextCtrl :: AppendText)。目前爲止,我有2個源文件和2個頭文件,用於wx程序本身(不包括我自己的libs,MySQL lib等),說MainDlg.cpp包含一個名爲'MainDlg'的MainForm的派生類wxFrame .cpp其中包含一個叫做'MainForm'的派生類wxApp。wxWidgets GUI應用程序中的多線程?
MainForm.cpp
#include "MainHeader.h" // contains multiple header files
IMPLEMENT_APP(MainForm)
bool MainForm::OnInit()
{
MainDlg *Server = new MainDlg(wxT("App Server 1.0"), wxDEFAULT_FRAME_STYLE - wxRESIZE_BORDER - wxMAXIMIZE_BOX);
Editor->Show();
return true;
}
MainDlg.cpp:
#include "MainHeader.h"
BEGIN_EVENT_TABLE(MainDlg, wxFrame)
EVT_BUTTON(6, MainDlg::StartServer)
EVT_BUTTON(7, MainDlg::StopServer)
END_EVENT_TABLE()
CNETServerConnection *cnServCon;
std::string ServerIP, DBHost, DBUser, DBName, DBPass;
int UserCapacity, DBPort, ServerPort;
MYSQL *sqlhnd;
MainDlg::MainDlg(const wxString &title, long style) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(301, 230), style)
{
cnServCon = new CNETServerConnection(100);
this->InitializeComponent();
}
void MainDlg::InitializeComponent()
{
this->SetTitle(wxT("App Server 1.0"));
this->SetSize(396, 260);
this->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
this->Centre();
statBox = new wxTextCtrl(this, 4, wxT("Welcome to AppServer 1.0\n\n"), wxPoint(10, 10), wxSize(371, 141), wxTE_MULTILINE | wxTE_READONLY);
//.................................
}
void MainDlg::StartServer(wxCommandEvent &event)
{
this->startBtn->Enable(false);
this->AppendStatus(wxT("\nLoading server configuration... "));
//.................................
this->AppendStatus(wxT("OK\n\nServer ready!\n\n"));
// When the server is ready, I need to run a thread
// that will update the statBox (through AppendStatus func or wxTextCtrl::AppendText directly)
// regularly without interrupting the GUI itself.
// Because the thread will contain a while-loop
// to make the program keep receiving message from clients.
this->startBtn->Hide();
this->stopBtn->Show();
this->cmdBtn->Enable();
this->cmdBox->Enable();
}
void MainDlg::StopServer(wxCommandEvent &event)
{
//...................................
}
void MainDlg::AppendStatus(const wxString &message)
{
statBox->AppendText(message);
}
// Well, here is the function I'd like to run in a new thread
void MainDlg::ListenForMessages()
{
int MsgSender = 0;
while(1)
{
if(!cnServCon->GetNewMessage())
continue;
if(MsgSender = cnServCon->GetJoiningUser())
this->AppendStatus(wxT("Someone connected to the server."));
}
}
我還發現wxThread的使用示例從Simple example of threading in C++:
class MessageThread : public wxThread
{
private:
MessageThread(const MessageThread ©);
public:
MessageThread() : wxThread(wxTHREAD_JOINABLE)
{
}
void *Entry(void)
{
// My works goes here
return;
}
};
wxThread *CreateThread()
{
wxThread *_hThread = new MessageThread();
_hThread->Create();
_hThread->Run();
return _hThread;
}
但我不知道如何將其與我的程序相關聯,並使其能夠修改我的GUI屬性(statBox)。
任何形式的幫助,將不勝感激! :)
謝謝。
請注意,'Entry'返回'ExitCode',而不是'void *'。所以請確保你重寫正確的方法(使用'ExitCode')。 – 2009-10-05 18:25:37
我也想警告你'wxThread'的等待函數。在Windows上,它的等待會阻止*,但仍處理任意事件*。所以可能會發生這樣的情況:在一個事件處理程序中,您調用等待一個可連接的線程,並在等待期間處理事件,然後它調用另一個事件處理程序(以wx術語稱爲「yielding」),這個恕我直言是完全愚蠢的。這是我一直使用另一個線程類修復它的主要原因,從來沒有'wxThread'。 – 2009-10-05 20:45:33