1 GetExitCodeThread
BOOL GetExitCodeThread(
HANDLE hThread,
LPDWORD lpExitCode
);
说明:获取一个已中止线程的退出代码
hThread:想获取退出代码的一个线程的句柄
lpExitCode:用于装载线程退出代码的一个长整数变量。如线程尚未中断,则设为常数STILL_ACTIVE
2 ThreadProc
DWORD WINAPI ThreadProc(LPVOID lpParameter);
说明:定义一个函数作为一个线程的起始服务地址。
lpParameter:接收CreateThread传递的参数(lpParameter)
3CreateThread
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize,
LPHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadid
);
lpThreadAttributes:指向SECURITY_ATTRIBUTES型态的结构的指针。在Windows NT中,它被设为NULL,表示使用缺省值。
dwStackSize:线程堆栈大小,一般为0,在任何情况下,Windows根据需要动态延长堆栈的大小。
lpStartAddress:指向线程函数的指针。
lpParameter:向线程函数传递的参数,是一个指向结构的指针,不需传递参数时,为NULL。
dwCreateFlags:线程标志,可取值CREATE_SUSPENDED(挂起的线程),0表示立即激活。
lpThreadid保存新线程的id
DWORD WINAPI ThreadFun(LPVOID pThread) { lpthread temp=(lpthread)pThread; temp->progress->SetPos(temp->pos); while(temp->pos<20) { Sleep(temp->speed); temp->pos++; temp->progress->SetPos(temp->pos); if(temp->pos==20) { temp->pos=0; } } return true; } void CCreateThreadDlg::OnStart1() { // TODO: 在此添加控件通知处理程序代码 DWORD ThreadID; DWORD code; thread1.progress=&m_progress1; thread1.speed=100; thread1.pos=0; if (!GetExitCodeThread(hThread1,&code)||(code!=STILL_ACTIVE)) { hThread1=CreateThread(NULL,0,ThreadFun,&thread1,0,&ThreadID);//创建并开始线程 } GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE); } void CCreateThreadDlg::OnStart2() { // TODO: 在此添加控件通知处理程序代码 DWORD ThreadID; DWORD code; thread2.progress=&m_progress2; thread2.speed=200; thread2.pos=0; if (!GetExitCodeThread(hThread2,&code)||(code!=STILL_ACTIVE)) { hThread2=CreateThread(NULL,0,ThreadFun,&thread2,0,&ThreadID); } GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE); } void CCreateThreadDlg::OnStart3() { // TODO: 在此添加控件通知处理程序代码 DWORD ThreadID; DWORD code; thread3.progress=&m_progress3; thread3.speed=200; thread3.pos=0; if(!GetExitCodeThread(hThread3,&code)||(code!=STILL_ACTIVE)) { hThread3=CreateThread(NULL,0,ThreadFun,&thread3,0,&ThreadID); } GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON6)->EnableWindow(FALSE); } void CCreateThreadDlg::OnPause1() { // TODO: 在此添加控件通知处理程序代码 DWORD code; if(GetExitCodeThread(hThread1,&code)) { if(code==STILL_ACTIVE) { TerminateThread(hThread1,0); CloseHandle(hThread1); } } GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE); } void CCreateThreadDlg::OnPause2() { // TODO: 在此添加控件通知处理程序代码 DWORD code; if(GetExitCodeThread(hThread2,&code)) { if(code==STILL_ACTIVE) { TerminateThread(hThread2,0); CloseHandle(hThread2); } } GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE); } void CCreateThreadDlg::OnPause3() { // TODO: 在此添加控件通知处理程序代码 DWORD code; if (GetExitCodeThread(hThread3,&code)) { if(code==STILL_ACTIVE) { TerminateThread(hThread3,0); CloseHandle(hThread3); } } GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON6)->EnableWindow(TRUE); }