wxWidgets简单的多线程

#include 
#include 
#include event.h>
#include 
#include 

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();
//    wxMessageBox(t);
    int n=event.GetInt();
    g->SetValue(n);
}

 

转载于:https://www.cnblogs.com/tiandsp/p/7440806.html

你可能感兴趣的:(wxWidgets简单的多线程)