GUI 在多线程应用程序的 Wxwidgets 中没有响应
我已经为这段代码苦苦挣扎了好几天。该程序是多线程的,新创建的线程会引发将数据写入主线程的事件。但程序是当线程调用事件时gui变得无响应。如果我用一些独立的消息框替换 status_brute_text->AppendText(wxT("insert")) ,那么程序运行不会出现问题。此外,线程同时作为可分离和可连接传递,但不会发生任何差异。我实际上计划使用 wxExecute 打开一个带有此线程的控制台应用程序,并将其输出输出到 gui。任何帮助将不胜感激。
提前谢谢你了......
testerFrame::testerFrame(wxFrame *frame)
: GUIFrame(frame)
{
#define thread_adder 10
#if wxUSE_STATUSBAR
statusBar->SetStatusText(_("John the ripper GUI"), 0);
statusBar->SetStatusText(wxbuildinfo(short_f), 1);
#endif
this->Connect(wxID_ANY,wxEVT_COMMAND_TEXT_UPDATED,
wxCommandEventHandler(testerFrame::insert));
}
testerFrame::~testerFrame()
{
}
void testerFrame::insert(wxCommandEvent &event)
{
status_brute_text->AppendText(wxT("insert"));
}
void testerFrame::OnClose(wxCloseEvent &event)
{
Destroy();
}
void testerFrame::OnQuit(wxCommandEvent &event)
{
Destroy();
}
void testerFrame::OnAbout(wxCommandEvent &event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}
void testerFrame::configure(wxCommandEvent &event)
{
wxString msg = wxT("Will be implemented later");
wxMessageDialog *dialog = new wxMessageDialog(0L,msg,_("hia"),wxYES_NO);
dialog->ShowModal();
}
void testerFrame::select_pass_file( wxCommandEvent& event )
{
if(m_filePicker2->GetPath().IsEmpty())
{
return;
}
}
void testerFrame::start_john(wxCommandEvent &event)
{
/*wxArrayString output,output2;
wxString command = wxString(wxT("john --incremental --session:jvc")) + m_filePicker2->GetPath();
wxMessageBox(command);
wxExecute(command,output);*/
MyThread *th = new MyThread(this);
th->Create();
th->Run();
}
void *MyThread::Entry()
{
// notify the main thread
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, wxID_ANY);
event.SetInt(1);
// pass some data along the event, a number in this case
m_parent->GetEventHandler()->AddPendingEvent( event );
return 0;
}
I've been struggling with this code for days. The program is multi-threaded with newly created threads raising events for writing data to main thread. But the program is that the gui becomes non-responsive when the thread calls the event. If I replace the status_brute_text->AppendText(wxT("insert")) with some message box which is independent, then the program runs without a problem. Also the thread is passed as both as detachable and joinable, but no difference occurs. I actually plan to open a console app with this thread with wxExecute and output its output to gui. Any help would be greatly appreciated.
Thanking you in advance....
testerFrame::testerFrame(wxFrame *frame)
: GUIFrame(frame)
{
#define thread_adder 10
#if wxUSE_STATUSBAR
statusBar->SetStatusText(_("John the ripper GUI"), 0);
statusBar->SetStatusText(wxbuildinfo(short_f), 1);
#endif
this->Connect(wxID_ANY,wxEVT_COMMAND_TEXT_UPDATED,
wxCommandEventHandler(testerFrame::insert));
}
testerFrame::~testerFrame()
{
}
void testerFrame::insert(wxCommandEvent &event)
{
status_brute_text->AppendText(wxT("insert"));
}
void testerFrame::OnClose(wxCloseEvent &event)
{
Destroy();
}
void testerFrame::OnQuit(wxCommandEvent &event)
{
Destroy();
}
void testerFrame::OnAbout(wxCommandEvent &event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}
void testerFrame::configure(wxCommandEvent &event)
{
wxString msg = wxT("Will be implemented later");
wxMessageDialog *dialog = new wxMessageDialog(0L,msg,_("hia"),wxYES_NO);
dialog->ShowModal();
}
void testerFrame::select_pass_file( wxCommandEvent& event )
{
if(m_filePicker2->GetPath().IsEmpty())
{
return;
}
}
void testerFrame::start_john(wxCommandEvent &event)
{
/*wxArrayString output,output2;
wxString command = wxString(wxT("john --incremental --session:jvc")) + m_filePicker2->GetPath();
wxMessageBox(command);
wxExecute(command,output);*/
MyThread *th = new MyThread(this);
th->Create();
th->Run();
}
void *MyThread::Entry()
{
// notify the main thread
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, wxID_ANY);
event.SetInt(1);
// pass some data along the event, a number in this case
m_parent->GetEventHandler()->AddPendingEvent( event );
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题:
AppendText()
生成一个wxEVT_COMMAND_TEXT_UPDATED
,这会导致调用testerFrame::insert()
并调用AppendText()
生成另一个wxEVT_COMMAND_TEXT_UPDATED
.....来自 wxWidgets 文档:
解决方案:
使用
ChangeValue( status_brute_text->GetValue() + "insert" )
而不是AppendText( "insert" )
。Problem:
AppendText()
generates awxEVT_COMMAND_TEXT_UPDATED
, which causestesterFrame::insert()
to be called which callsAppendText()
which generates anotherwxEVT_COMMAND_TEXT_UPDATED
.....From wxWidgets Documentation:
Solution:
Use
ChangeValue( status_brute_text->GetValue() + "insert" )
instead ofAppendText( "insert" )
.