GUI 在多线程应用程序的 Wxwidgets 中没有响应

发布于 2024-10-28 23:50:57 字数 2031 浏览 1 评论 0原文

我已经为这段代码苦苦挣扎了好几天。该程序是多线程的,新创建的线程会引发将数据写入主线程的事件。但程序是当线程调用事件时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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2024-11-04 23:50:57

问题:

AppendText() 生成一个 wxEVT_COMMAND_TEXT_UPDATED,这会导致调用 testerFrame::insert() 并调用AppendText() 生成另一个 wxEVT_COMMAND_TEXT_UPDATED.....

来自 wxWidgets 文档

...一个
wxEVT_COMMAND_TEXT_UPDATED 事件,
[is] 文本更改时生成。
请注意,此事件将被发送
当文本控制内容时
更改 - 这是否是由于用户造成的
输入或来自程序本身
(例如,如果 SetValue() 是
称为);请参阅 ChangeValue() 了解
不发送此内容的函数
活动

解决方案:

使用ChangeValue( status_brute_text->GetValue() + "insert" ) 而不是AppendText( "insert" )

Problem:

AppendText() generates a wxEVT_COMMAND_TEXT_UPDATED, which causes testerFrame::insert() to be called which calls AppendText() which generates another wxEVT_COMMAND_TEXT_UPDATED.....

From wxWidgets Documentation:

...a
wxEVT_COMMAND_TEXT_UPDATED event,
[is] generated when the text changes.
Notice that this event will be sent
when the text controls contents
changes - whether this is due to user
input or comes from the program itself
(for example, if SetValue() is
called); see ChangeValue() for a
function which does not send this
event

Solution:

Use ChangeValue( status_brute_text->GetValue() + "insert" ) instead of AppendText( "insert" ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文