wxThread 不知道是否创建成功? Topic is solved

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

wxThread 不知道是否创建成功?

Post by hats » Mon Jun 14, 2010 4:07 pm

有个函数很费时间,直接在OnButton事件中会堵塞GUI界面,使程序失去反映。
试了一下wxThread,但使用后用wndspy发现程序还是一个线程,而且还会堵塞GUI界面。大家看下这样使用wxThread正确吗?

Code: Select all

class MyThread : public wxThread
{
public:
    MyThread() : wxThread(){}
    ~MyThread() {}

    virtual ExitCode Entry()
    {
        return 0;
    }

    void Test();  //测试函数
};

class wxThreadDialog: public wxDialog
{
public:

    wxThreadDialog(wxWindow* parent,wxWindowID id = -1);
    virtual ~wxThreadDialog();

private:

    //(*Handlers(wxThreadDialog)
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    void OnButton1Click(wxCommandEvent& event);
    void OnButton2Click(wxCommandEvent& event);
    //*)

    //(*Identifiers(wxThreadDialog)
    static const long ID_STATICTEXT1;
    static const long ID_BUTTON1;
    //*)

    //(*Declarations(wxThreadDialog)
    wxButton* Button1;
    wxStaticText* StaticText1;
    //*)
    MyThread* thread; /////////////////////////wxThread

    DECLARE_EVENT_TABLE()
};

wxThreadDialog::wxThreadDialog(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(wxThreadDialog)
    Create(parent, id, _("wxWidgets app"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX, _T("id"));
    SetClientSize(wxSize(412,242));
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Label"), wxPoint(24,48), wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    Button1 = new wxButton(this, ID_BUTTON1, _("Button"), wxPoint(104,152), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxThreadDialog::OnButton1Click);
    //*)

    thread = new MyThread;

    if(thread->Create() == wxTHREAD_NO_ERROR)
    {
        thread->Run(); /////////////////////////Run
    }
    else
    {
        wxMessageBox("Thread create False!");
    }
}


void MyThread::Test()   ////////////////////测试函数
{
    wxMessageBox("Hello wxThread!");
}


void wxThreadDialog::OnButton1Click(wxCommandEvent& event)
{
    thread->Test();/////////////调用wxThread中的函数,能弹出对话框,但换成复杂的函数还是会堵塞GUI界面,用wndspy查看程序还是一个线程
}








hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats » Tue Jun 15, 2010 3:50 pm

没人么?
给个简单的wxThread例子也行啊

samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 324
Joined: Mon Oct 06, 2008 12:55 pm

Re: wxThread 不知道是否创建成功?

Post by samsam598 » Thu Jun 17, 2010 12:46 am

hats wrote: void wxThreadDialog::OnButton1Click(wxCommandEvent& event)
{
thread->Test();/////////////调用wxThread中的函数,能弹出对话框,但换成复杂的函数还是会堵塞GUI界面,用wndspy查看程序还是一个线程
}
[/code]
这是俺能给你的一点点提示:

wxWidgets allows GUI calls only from the main thread. So the "official" wxwidgets way would be to send an event to the main thread and make the GUI update from there.

Alternatively you can try wrapping your GUI calls in wxMutexGuiEnter/wxMutexGuiLeave, but it's not guaranteed to work in all cases.

http://docs.wxwidgets.org/stable/wx_thr ... exguienter
Regards,
Sam
-------------------------------------------------------------------
Windows xp
VS.Net 2003/MinGW 3.4.5 C::B character set: UTF-8
wxWidgets github 3.0 RC1 Unicode Static build,Unicode Shared build.

Post Reply