Windows中OnTimer和執行緒

軍說網事發表於2015-03-31

首先看看OnTimer的做法。

1 在類中

void CMyDialog::OnTimer(UINT nIDEvent)

{

    if (nIDEvent == MY_TIMER)

{

    //process something

    }

        

         CDialog::OnTimer(nIDEvent);

}

 

BOOL CODM_DlgShowImage::OnInitDialog()

{

    SetTimer(MY_TIMER, 500, NULL);

}

 

2

void CMyApp::CreateDlg ()

{   

         if (m_pDlg)

         {

                   m_pDlg->DestroyWindow();

                   delete m_pDlg;

                   m_pDlg = NULL;

         }

 

         m_pDlg = new CMyDialog ();

         m_ pDlg ->Create(CMyDialog::IDD);

         m_ pDlg ->SetContainer(&m_container);

         m_ pDlg ->ShowWindow(SW_HIDE);  

}

 

這樣做有什麼問題呢,我們先不說,然後看看用執行緒的做法。

 

void CMyApp::CreateDlg ()

{   

         if (m_pDlg)

         {

                   m_pDlg->DestroyWindow();

                   delete m_pDlg;

                   m_pDlg = NULL;

         }

 

         m_pDlg = new CMyDialog ();

         m_ pDlg ->Create(CMyDialog::IDD);

         m_ pDlg ->SetContainer(&m_container);

         m_ pDlg ->ShowWindow(SW_HIDE);

   

    AfxBeginThread(ThreadProc, this);

}

 

UINT CMyApp::ThreadProc(void* p)

{

         CMyApp * CApp = (CMyApp *)p;

 

         while(1)

         {

             if ()

        {

            //process something;

        }

                   else

        {

                            break;

                           

        }

                   Sleep(200);

         }

 

         return 0;

}

 

採用執行緒的方式解決了什麼問題呢,大家可以思考一下。

 

轉載請註明原創連線:http://blog.csdn.net/wujunokay/article/details/44783473

 

相關文章