欢迎分享,转载请注明来源:内存溢出
windows多线程编程技术与实例_windows实现多线程编程方法
1 创建式程,编辑对话框资源创建一个基于对话框的工程,添加控件,如下图所示:各控件ID及变量如下:2 在头文件中定义与线程相关变量// Ch13Demo2Dlg.htypedef struct Threadinfo{CProgressCtrl *progress;//进度条对象int speed;//进度条速度int pos;//进度条位置} thread,*lpthread;class CCh13Demo2Dlg : public CDialog{……protected:HICON m_hIcon;thread thread1;//线程1的结构thread thread2;//线程2的结构thread thread3;//线程3的结构HANDLE hThread1;//线程1线程句柄HANDLE hThread2;//线程2线程句柄HANDLE hThread3;//线程3线程句柄定义线程入口函数// Ch13Demo2Dlg.cppDWORD 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;//进度条满则归0}}return true;}3 对话框控件初始化// Ch13Demo2Dlg.cppBOOL CCh13Demo2Dlg::OnInitDialog(){BOOL CCh13Demo2Dlg::OnInitDialog(){CDialog::OnInitDialog();……m_progress1.SetRange(0,20);//设置进度条范围m_progress2.SetRange(0,20);//设置进度条范围m_progress3.SetRange(0,20);//设置进度条范围GetDlgItem(IDC_PAUSE1)->EnableWindow(FALSE);//停止按钮无效GetDlgItem(IDC_PAUSE2)->EnableWindow(FALSE);//停止按钮无效GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按钮无效return TRUE;}// Ch13Demo2Dlg.cppDWORD 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;//进度条满则归0}}return true;}void CCh13Demo2Dlg::OnStar1(){// TODO: Add your control notification handler code hereDWORD 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_PAUSE1)->EnableWindow(TRUE);//停止按钮生效GetDlgItem(IDC_STAR1)->EnableWindow(FALSE);//开始按钮无效}void CCh13Demo2Dlg::OnStar2(){// TODO: Add your control notification handler code hereDWORD 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_PAUSE2)->EnableWindow(TRUE);//停止按钮生效GetDlgItem(IDC_STAR2)->EnableWindow(FALSE);//开始按钮无效}void CCh13Demo2Dlg::OnStar3(){// TODO: Add your control notification handler code hereDWORD 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_PAUSE3)->EnableWindow(TRUE);//停止按钮生效GetDlgItem(IDC_STAR3)->EnableWindow(FALSE);//开始按钮无效}void CCh13Demo2Dlg::OnPause1(){// TODO: Add your control notification handler code hereDWORD code;if(GetExitCodeThread(hThread1,&code))if(code==STILL_ACTIVE)//如果当前线程还活动{TerminateThread(hThread1,0);//前些终止线程CloseHandle(hThread1);//销毁线程句柄}GetDlgItem(IDC_PAUSE1)->EnableWindow(FALSE);//停止按钮无效GetDlgItem(IDC_STAR1)->EnableWindow(TRUE);//开始按钮生效}void CCh13Demo2Dlg::OnPause2(){// TODO: Add your control notification handler code hereDWORD code;if(GetExitCodeThread(hThread2,&code))if(code==STILL_ACTIVE){TerminateThread(hThread2,0);CloseHandle(hThread2);}GetDlgItem(IDC_PAUSE2)->EnableWindow(FALSE);//停止按钮无效GetDlgItem(IDC_STAR2)->EnableWindow(TRUE);//开始按钮生效}void CCh13Demo2Dlg::OnPause3(){// TODO: Add your control notification handler code hereDWORD code;if(GetExitCodeThread(hThread3,&code))if(code==STILL_ACTIVE){TerminateThread(hThread3,0);CloseHandle(hThread2);}GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按钮无效GetDlgItem(IDC_STAR3)->EnableWindow(TRUE);//开始按钮生效}
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
vbscript运算符有哪几种_有关vbscript运算符知识解读
上一篇
2022-04-17
php数据库连接失败的原因_出错原因和解决方法
下一篇
2022-04-17
评论列表(0条)