windows实现多线程编程方法 windows多线程编程技术与实例
1 创建式程,编辑对话框资源创建一个基于对话框的工程,添加控件,如下图所示:
![windows实现多线程编程方法 windows多线程编程技术与实例](https://www.changchenghao.cn/wp-content/uploads/2021/07/594800008e86d776a693.png)
文章插图
各控件ID及变量如下:
![windows实现多线程编程方法 windows多线程编程技术与实例](https://www.changchenghao.cn/wp-content/uploads/2021/07/5e7700006203691de690.png)
文章插图
2 在头文件中定义与线程相关变量
// Ch13Demo2Dlg.h定义线程入口函数
typedef 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.cpp3 对话框控件初始化
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;//进度条满则归0
}
}
return true;
}
// Ch13Demo2Dlg.cpp
BOOL 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.cpp
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;//进度条满则归0
}
}
return true;
}
void CCh13Demo2Dlg::OnStar1()
{
// TODO: Add your control notification handler code here
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_PAUSE1)->EnableWindow(TRUE);//停止按钮生效
GetDlgItem(IDC_STAR1)->EnableWindow(FALSE);//开始按钮无效
}
void CCh13Demo2Dlg::OnStar2()
{
// TODO: Add your control notification handler code here
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_PAUSE2)->EnableWindow(TRUE);//停止按钮生效
GetDlgItem(IDC_STAR2)->EnableWindow(FALSE);//开始按钮无效
}
void CCh13Demo2Dlg::OnStar3()
{
// TODO: Add your control notification handler code here
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_PAUSE3)->EnableWindow(TRUE);//停止按钮生效
GetDlgItem(IDC_STAR3)->EnableWindow(FALSE);//开始按钮无效
}
void CCh13Demo2Dlg::OnPause1()
{
// TODO: Add your control notification handler code here
DWORD 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 here
DWORD 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 here
DWORD code;
if(GetExitCodeThread(hThread3,&code))
if(code==STILL_ACTIVE)
{
TerminateThread(hThread3,0);
CloseHandle(hThread2);
}
GetDlgItem(IDC_PAUSE3)->EnableWindow(FALSE);//停止按钮无效
GetDlgItem(IDC_STAR3)->EnableWindow(TRUE);//开始按钮生效
【windows实现多线程编程方法 windows多线程编程技术与实例】}
![windows实现多线程编程方法 windows多线程编程技术与实例](https://www.changchenghao.cn/wp-content/uploads/2021/07/594400011c7a822e3c95.png)
文章插图
推荐阅读
- 米酒发酵时间是多久 米酒发酵多少时间
- 原汤化原食喝点饺子汤好处超多
- 烤土豆竟有这些好处
- 土豆丝这样炒才营养
- 饭量太小破坏肠道菌群平衡
- 吃肉太多易老化 7个坏习惯降低免疫力
- 火龙果吃多了会怎么样 这些你知道吗
- 在他乡时,你被外省人问过最多的问题是什么?
- 三国后期蜀国灭亡后,吴国如何撑住了多年不被灭亡?
- 低温天气行车轮胎胎压多少合适