wxWidgets简单的多线程

#include <wx/wx.h>
#include <wx/thread.h>
#include <wx/event.h>
#include <wx/progdlg.h>
#include <wx/gauge.h>

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
    void OnAddText(wxThreadEvent &event);

    wxGauge *g;
};

class MyThread : public wxThread
{
public:
    MyThread(wxWindow *w)
    {
        theParent=w;
    };
    virtual void* Entry();

    wxWindow *theParent;
};

void* MyThread::Entry(){
    wxThreadEvent e(wxEVT_THREAD);//declared and implemented somewhere
//    wxString text("I am sent!");
//    e.SetPayload(wxString::Format("%s", text.c_str()));
    for (int i=0;i<100;i++)
    {
        if (TestDestroy())
        {
            break;
        }
        e.SetInt(i);
        wxMilliSleep(20);
        theParent->GetEventHandler()->AddPendingEvent(e);
    }
    return NULL;
}


IMPLEMENT_APP(MyApp)

    bool MyApp::OnInit()
{

    wxFrame *f = new wxFrame(NULL,wxID_ANY,wxT("test"));
    Connect(wxID_ANY, wxEVT_THREAD, wxThreadEventHandler(MyApp::OnAddText), NULL, this);//connect event to a meth

    wxPanel *p=new wxPanel(f,wxID_ANY,wxDefaultPosition,wxDefaultSize);

    g=new wxGauge(p,wxID_ANY,100);
    f->Show(true);

    MyThread *th=new MyThread(f);
    th->Create();
    th->Run();
    return true;
}


void MyApp::OnAddText(wxThreadEvent& event) {
//    wxString t = event.GetPayload<wxString>();
//    wxMessageBox(t);
    int n=event.GetInt();
    g->SetValue(n);
}

 

posted @ 2017-08-27 17:02  Dsp Tian  阅读(1564)  评论(0编辑  收藏  举报