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;
     
}

运行程序

相关推荐
剑锋所指,所向披靡!1 小时前
C++之类模版
java·jvm·c++
C+-C资深大佬2 小时前
C++风格的命名转换
开发语言·c++
No0d1es2 小时前
2025年粤港澳青少年信息学创新大赛 C++小学组复赛真题
开发语言·c++
点云SLAM2 小时前
C++内存泄漏检测之手动记录法(Manual Memory Tracking)
开发语言·c++·策略模式·内存泄漏检测·c++实战·new / delete
好评1242 小时前
【C++】二叉搜索树(BST):从原理到实现
数据结构·c++·二叉树·二叉搜索树
zylyehuo2 小时前
error: no matching function for call to ‘ros::NodeHandle::param(const char [11], std::string&, const char [34])’
c++·ros1
星火开发设计3 小时前
C++ 函数定义与调用:程序模块化的第一步
java·开发语言·c++·学习·函数·知识
天赐学c语言4 小时前
1.20 - x的平方根 && vector的扩容机制以及删除元素是否会释放内存
c++·算法·leecode
CC.GG4 小时前
【C++】用哈希表封装myunordered_map和 myunordered_set
java·c++·散列表
xiaoye-duck5 小时前
C++ string 类使用超全攻略(上):创建、遍历及容量操作深度解析
c++·stl