[WxWidgets] _ [primary] _ [common update progress bar solution]

Source: Internet
Author: User


Scenario:

1. many programs need to update the progress bar based on the business processing progress. The progress bar aims to allow users to know the business processing progress. Programs with progress bars are more friendly, so that users can know that the program is running, instead of no response.

2. currently, we have seen two ways to update the progress bar. One is to let the main thread update the progress bar in the order of sending events (signals, queues, one is to set a global integer variable,

Update the progress bar by running the timer. The first method is not suitable for scenarios with high update frequency. For example, if 20 events are sent in one second, the main thread is busy processing the event interface and the status is suspended.

Therefore, the best way is to use the timer to update the progress bar and set a reasonable value. For example, 2nd milliseconds can both view the update progress and avoid the master thread being too busy.

3. Here we use wxWidgets as an example. In fact, both MFC, wtl, and cocoa are the same.


Some code:

My_thread.cpp

# Include "domain/my_thread.h" # include "window/my_frame.h" wxthread: exitcode mythread: entry () {sleep (500); int I = 1; while (! Testdestroy () & I <= 100) {If (Type _) {// The first method sends an event update progress bar. Note that you cannot directly update the progress bar control, because the non-main thread cannot update the control wxcommandevent * EVT = new wxcommandevent (kernel); EVT-> setint (I); wxqueueevent (m_phandler-> geteventhandler (), EVT );} else {// Method 2: directly update the int member variable in myframe. However, wxtimer will automatically update the progress bar Control Based on this value. m_phandler-> progress_value _ = I; sleep (200) ;}++ I ;}// 1. finally, it is necessary to tell wxtimer that it can end. In short, the task of notifying the interface has been completed and you should do it yourself. Wxqueueevent (m_phandler-> geteventhandler (), new wxcommandevent (wxevt_command_mythread_completed); Return (wxthread: exitcode) 0 ;}

My_frame.cpp

#include "window/my_frame.h"#include "wx/xrc/xmlres.h"#include <iostream>#include "domain/my_thread.h"using namespace std;BEGIN_EVENT_TABLE(MyFrame, wxFrame)EVT_COMMAND(wxID_ANY, wxEVT_COMMAND_MYTHREAD_UPDATE, MyFrame::OnThreadUpdate)    EVT_COMMAND(wxID_ANY, wxEVT_COMMAND_MYTHREAD_COMPLETED,MyFrame::OnThreadCompletion)EVT_TIMER(wxID_ANY,MyFrame::OnTimer)END_EVENT_TABLE()wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_COMPLETED, wxThreadEvent);wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_UPDATE, wxThreadEvent);void MyFrame::Init(){   gauge_ =  XRCCTRL(*this, "m_gauge1", wxGauge);   event_update_button_ = XRCCTRL(*this, "m_button1", wxButton);   event_update_button_->Bind(wxEVT_COMMAND_BUTTON_CLICKED,        wxCommandEventHandler(MyFrame::OnEventUpdate), this, XRCID("m_button1"));   timer_update_button_ = XRCCTRL(*this, "m_button3", wxButton);   timer_update_button_->Bind(wxEVT_COMMAND_BUTTON_CLICKED,        wxCommandEventHandler(MyFrame::OnTimerUpdate), this, XRCID("m_button3"));   timer.SetOwner(this->GetEventHandler());}MyFrame::MyFrame(wxWindow* parent){progress_value_ = 0;}MyFrame::~MyFrame(){}void MyFrame::OnThreadUpdate(wxCommandEvent& evt){cout << "OnThreadUpdate" << endl;gauge_->SetValue(evt.GetInt());}void MyFrame::OnThreadCompletion(wxCommandEvent&){cout << "OnThreadCompletion" << endl;if(!type_){timer.Stop();gauge_->SetValue(progress_value_);}timer_update_button_->Enable();event_update_button_->Enable();}void MyFrame::OnTimer(wxTimerEvent&){cout << "OnTimer" << endl;gauge_->SetValue(progress_value_);}void MyFrame::DoTask(bool type){timer_update_button_->Enable(false);event_update_button_->Enable(false);    MyThread *m_pThread = new MyThread(type,this);    if ( m_pThread->Run() != wxTHREAD_NO_ERROR )    {        cout << "Can't create the thread!: " << type << endl;        delete m_pThread;       m_pThread = NULL;   }}void MyFrame::OnTimerUpdate(wxCommandEvent& evt){cout << "OnTimerUpdate" << endl;gauge_->SetValue(0);progress_value_ = 0;type_ = false;timer.Start(500);DoTask(type_);}void MyFrame::OnEventUpdate(wxCommandEvent& evt){    cout << "OnEventUpdate" << endl;gauge_->SetValue(0);progress_value_ = 0;type_ = true;DoTask(type_);}

Gui. cpp

#include "wx/wxprec.h"#ifndef WX_PRECOMP    #include "wx/wx.h"#endif#include "wx/xrc/xmlres.h"#include <assert.h>#include <iostream>#include <stdio.h>#include <io.h>#include <fcntl.h>#include "wx/listctrl.h"#include "wx/grid.h"#include "wx/event.h"#include "window/my_frame.h"using namespace std;static void OpenConsole(){    AllocConsole();    HANDLE   handle   =   GetStdHandle(STD_OUTPUT_HANDLE);    int   hCrt   =   _open_osfhandle((long)handle,_O_TEXT);    FILE   *   hf   =   _fdopen(   hCrt,   "w"   );    *stdout   =   *hf;    }static wxString GetAppRunDirectory(){    wxString directory(wxTheApp->argv[0]);    directory.Replace(wxT("\\"), wxT("/"));    wxString str = directory.BeforeLast('/');    if(str.IsEmpty())    {      return wxT(".");    }    return directory.BeforeLast('/');}static wxString GetInstallDirectory(){wxString directory = GetAppRunDirectory();#ifdef __QXWIN32__    return directory.BeforeLast('/');#else    return directory;#endif}class MyApp : public wxApp{public:    virtual bool OnInit();    virtual int OnExit();};IMPLEMENT_APP(MyApp)// 'Main program' equivalent: the program execution "starts" herebool MyApp::OnInit(){  if (!wxApp::OnInit())  {return false;  }  OpenConsole();  wxInitAllImageHandlers();  wxXmlResource::Get()->InitAllHandlers();    wxString ui_dir = GetInstallDirectory()+wxT("/Themes");  wxXmlResource::Get()->LoadAllFiles(ui_dir);  MyFrame *frame = new MyFrame(NULL);  bool loaded = wxXmlResource::Get()->LoadFrame(frame, NULL, "MyFrame1");  assert(loaded);  frame->Init();  frame->Show(true);  return true;}int MyApp::OnExit(){return 0;}

:


Complete code:

Http://download.csdn.net/detail/infoworld/8105011


[WxWidgets] _ [primary] _ [common update progress bar solution]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.