C++/MFC 创建线程
1.用AfxBeginThread创建线程,AfxEndThread终止线程
void CCreateThreadDlg::SetEditText() { GetDlgItem(IDC_EDIT1)->SetWindowText("test"); } CWinThread *Thread; UINT ThreadProc(LPVOID lParam) { CCreateThreadDlg *pThreadDlg = (CCreateThreadDlg *)lParam; pThreadDlg->SetEditText(); DWORD dwExitCode; GetExitCodeThread(Thread, &dwExitCode); AfxEndThread(dwExitCode, TRUE); return 0; } void CCreateThreadDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //CDialogEx::OnOK(); Thread = AfxBeginThread(ThreadProc, (LPVOID)this); }
2.用CreateThread创建线程
void CCreateThreadDlg::SetEditText(CString strText) { GetDlgItem(IDC_EDIT1)->SetWindowText(strText); } HANDLE hThread; DWORD WINAPI ThreadProc(LPVOID lParam) { CCreateThreadDlg *pThreadDlg = (CCreateThreadDlg *)lParam; pThreadDlg->SetEditText("Thread"); return 0; } void CCreateThreadDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //CDialogEx::OnOK(); hThread =CreateThread(NULL,0, ThreadProc,this,0,NULL); CloseHandle(hThread); Sleep(1000); SetEditText("Main"); }