Progress bar Design for multiple threads in MFC

Source: Internet
Author: User

1. Create a New thread for many heavy tasks, and then create a worker thread. Then, worker can perform operations and set the display of the main thread modal dialog box.

UINT ProcessDlgFunc(LPVOID in){    if(!in)    {        return 100;    }    Sleep(3000);    ProcessDlg *dlg = (ProcessDlg *)in;    int count = 0;    while(count++ <= 100)    {        dlg->PostMessageW(WM_PROCESS, count);        Sleep(500);    }    dlg->PostMessageW(WM_QUIT);    return 200;}void CMFCApplication2View::OnEdit32771(){    // TODO: Add your command handler code here    AfxBeginThread(ProcessDlgFunc, dlg);    dlg->DoModal();}

Here, the new thread needs to do a job: first get up before domodel, and then wait for the dialog box to complete domodel. Finding a job is just a rough design, then, the new thread regularly sets the progress for the post message in the dialog box, and the multi-doubt dialog box also needs to complete message processing:

#define WM_PROCESS (WM_USER + 200)class ProcessDlg : public CDialogEx{DECLARE_DYNAMIC(ProcessDlg)public:ProcessDlg(CWnd* pParent = NULL);   // standard constructorvirtual ~ProcessDlg();// Dialog Dataenum { IDD = IDD_DIALOG1 };protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV supportDECLARE_MESSAGE_MAP()public:    CProgressCtrl m_Process;    virtual BOOL OnInitDialog();    afx_msg LRESULT do_process(WPARAM wParam,LPARAM lParam)  ;//    afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);};IMPLEMENT_DYNAMIC(ProcessDlg, CDialogEx)ProcessDlg::ProcessDlg(CWnd* pParent /*=NULL*/): CDialogEx(ProcessDlg::IDD, pParent){}ProcessDlg::~ProcessDlg(){}void ProcessDlg::DoDataExchange(CDataExchange* pDX){    CDialogEx::DoDataExchange(pDX);    DDX_Control(pDX, IDC_PROGRESS1, m_Process);}BEGIN_MESSAGE_MAP(ProcessDlg, CDialogEx)//    ON_WM_DRAWITEM()    ON_MESSAGE(WM_PROCESS, do_process)END_MESSAGE_MAP()LRESULT ProcessDlg::do_process(WPARAM wParam,LPARAM lParam)  {          //AfxMessageBox(_T("test"));      if(wParam >= 0 && wParam <= 100)        m_Process.SetPos(wParam);    return 0;}  // ProcessDlg message handlersBOOL ProcessDlg::OnInitDialog(){    CDialogEx::OnInitDialog();    // TODO:  Add extra initialization here    m_Process.SetRange(0, 100);    m_Process.SetStep(1);    return TRUE;  // return TRUE unless you set the focus to a control    // EXCEPTION: OCX Property Pages should return FALSE}

2. This method does not send messages, but uses a global value between the master and slave threads, such as an integer. The master thread uses a timer to rotate the value to set the progress of the progress bar, the new thread is responsible for completing background tasks and setting progress to shared values, which is also feasible.

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.