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);//开始按钮生效}
- 随机文章
- 核心危机(核心危机魔石合成攻略)
- 风儿(风儿轻轻的吹)
- 饿了么红包怎么用(饿了么红包怎么用微信支付)
- 光遇花手先祖位置(安卓光遇手花先祖)
- 抖音卡(抖音卡顿怎么解决)
- xboxones(xboxone手柄怎么配对主机)
- 陈武简历
- 帆船比赛(帆船比赛视频)
- 海猫鸣泣之时游戏(海猫鸣泣之时游戏在哪玩)
- 韩国媳妇和小雪(韩国媳妇和小雪的父亲工资是多少)
- 儋州市第二中学(儋州市第二中学录取分数线)
- 鬼泣5攻略(鬼泣5攻略第三关怎么跳)
- 地球日主题(2020年世界地球日主题)
- 和柳亚子(和柳亚子先生于田)
- 冰客(冰客果汁)
- yy魔兽(yy魔兽世界)
- 国外成人游戏(国外成人游戏注册需要visa信用卡)
- 充值卡代理(充值卡代理加盟)
- 拆奶罩
- 郭妮小说(恶魔的法则郭妮小说)
- 东天目山(东天目山景区)
- 杭同(杭同培训中心怎么样)
- 大松电饭煲(美的大松电饭煲)
- 服饰加盟(服饰加盟店招商)
- 疯狂填字(疯狂填字5)
- 观音普门品(观音普门品念诵全文)
- 河北省大运会(河北省大运会时间)
- 哈利波特官网(哈利波特官网在哪里)
- 杜星霖(杜星霖图片)
- 查传倜(查传倜个人生活)
