【发布时间】:2010-12-04 00:55:50
【问题描述】:
我在尝试为我的 wxWidgets GUI 应用程序创建一个将修改 GUI 属性本身之一的工作线程时遇到问题(基本上,我很困惑。) (在这种情况下,wxTextCtrl::AppendText)。
到目前为止,我有 wx 程序本身的 2 个源文件和 2 个头文件(不包括我自己的库、MySQL 库等),比如 MainDlg.cpp,它包含一个名为“MainDlg”和 MainForm 的 wxFrame 派生类。 cpp 包含一个名为“MainForm”的 wxApp 派生类。
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."));
}
}
我还从Simple example of threading in C++找到了一个wxThread的使用示例:
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)。 -
我还想警告你
wxThread的等待功能。在 Windows 上,它的等待会阻塞,但仍会处理任意事件。所以它可能发生在事件处理程序中,您在可连接线程上调用等待,它在等待期间处理事件,然后它调用另一个事件处理程序(在 wx 术语中称为“yielding”),恕我直言,这是完全愚蠢的。这就是我一直使用另一个修复它的线程类的主要原因,从来没有wxThread。
标签: c++ multithreading wxwidgets