MFC终止线程实例

本程序基于前期我的博客文章《MFC用信号灯模拟工控机数字量输入信号实时采集实例(源码下载》

1、在主界面添加一个启动线程按钮,一个终止线程按钮。

2、在TheradDlg.h中相关代码

cpp 复制代码
class CTheradDlg : public CDialog
{
// Construction
public:	
	...   
  	CTheradDlg(CWnd* pParent = NULL);	// standard constructor  
    CWinThread* pThread; 
	bool isThreadBegin ;  //线程退出/执行控制
	void My_Func();
  static UINT My_TheradFunc(LPVOID lparam);
  ...
  }

3、在TheradDlg.cpp中相关代码

cpp 复制代码
void CTheradDlg::OnButton1() 
{
isThreadBegin  = true;            //运行线程执行
pThread = AfxBeginThread(My_TheradFunc,(LPVOID)this);

}

void CTheradDlg::OnButton2() 
{
isThreadBegin  = false;            //停止线程执行	
}

UINT CTheradDlg::My_TheradFunc(LPVOID lparam)//将窗口指针赋给无类型指针
 
{
 
     CTheradDlg *dlg = (CTheradDlg*)lparam;   //可以调用CTheradtDlg定义的类成员函数
        
    while (dlg-> isThreadBegin)
	{
		dlg->My_Func(); //线程要执行的函数(调用CTheradtDlg定义的类成员函数)
		
	}
 return TRUE;
     
}

运行程序

相关推荐
无忧.芙桃9 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
AvocadoZeroOneOI12 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
whitelbwwww14 小时前
cuBLAS--CUDA高性能线性代数计算库--第一部分
c++·线性代数·gpu算力·cuda c++
eggwyw17 小时前
C++设计模式之解释器模式
开发语言·c++
我不会插花弄玉1 天前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
西西弗Sisyphus1 天前
C++ explicit 关键字工程实践
开发语言·c++
AIgorithmGEEK1 天前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
一拳一个呆瓜2 天前
【STL】头文件:<cmath>
c++·stl
草莓熊Lotso2 天前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构