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
);
实现过程
class CMfc01Dlg : public CDialog
{
// Construction
public:
CMfc01Dlg(CWnd* pParent = 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();
}
|
图
备注
相关链接
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。