C++ AfxBeginThread

 

计算从1+2+3...+100000=?

 

关键点

CWinThread* AfxBeginThread(

AFX_THREADPROC pfnThreadProc,

LPVOID pParam,

int nPriority = THREAD_PRIORITY_NORMAL,

UINT nStackSize = 0,

DWORD dwCreateFlags = 0,

LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL

);

UINT MyFunction( LPVOID pParam );

 

 

实现过程

 

 

class CMfc01Dlg : public CDialog
{
    // Construction
public:
    CMfc01Dlg(CWndpParent = NULL);    // standard constructor
    CWinThread *m_sumProc;
    // Dialog Data
    
    UINT SumPro(LPVOID pParam)
    {
        CMfc01Dlg *pDlg=(CMfc01Dlg*)pParam;
        __int64 nSum=0;
        char pszText[128]={0};
        for (UINT i=1;i<100000;i++)
        {
            nSum+=i;
            memset(pszText,0,128);
            wsprintf(pszText,"累计到%d的结果为%I64d",i,nSum);
            pDlg->m_listbox1.AddString(pszText);
        }
        return 0;
    } 
    
    void CMfc01Dlg::OnButton1() 
    {
        // TODO: Add your control notification handler code here
        m_sumProc=AfxBeginThread(SumPro,this,0,0,0,NULL);

 

void CMfc01Dlg::OnClose() 
{
    // TODO: Add your message handler code here and/or call default
    
    if (SumPro!=NULL)
    {
        DWORD dwExit=0;
        BOOL bRet=GetExitCodeThread(m_sumProc->m_hThread,&dwExit);
        if (dwExit==STILL_ACTIVE)
        {
            m_sumProc->ExitInstance();
            delete m_sumProc;
        }
    }
    
    CDialog::OnClose();

 

 

 

   


 

备注

 

 

相关链接

                           

 

 




原文地址:https://www.cnblogs.com/xe2011/p/3885681.html