zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    Length of Last Word
    Remove Duplicates from Sorted Array II
    Sum Root to Leaf Numbers
    Valid Parentheses
    Set Matrix Zeroes
    Symmetric Tree
    Unique Binary Search Trees
    110Balanced Binary Tree
    Match:Blue Jeans(POJ 3080)
    Match:Seek the Name, Seek the Fame(POJ 2752)
  • 原文地址:https://www.cnblogs.com/tiandsp/p/7440806.html
Copyright © 2011-2022 走看看