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

运行程序

相关推荐
可乐ea7 小时前
GitHub Security Lab 开源 Fuzzing Taskflow:让 LLM 智能体自己跑完 C/C++ 模糊测试
jvm·c++·github·模糊测试·mcp·代码agent·智能体自动化
朝朝辞暮i7 小时前
C++ 第 13 课:值传递 —— 为什么函数里改了,外面却没变?
java·c++·算法
无忧.芙桃8 小时前
C++语言原理与实践(十):vector类的底层实现
开发语言·c++·算法
无名猿8 小时前
std::variant 完全指南:类型安全的 union 与 std::visit 用法
c++·stl·标准库·现代c++
无名猿9 小时前
C++ 迭代器五分类与失效场景汇总:一张表避开所有 UB
c++·stl·标准库·踩坑记录
0+1119 小时前
算法 --模拟
c++·算法·leetcode
hetao17338379 小时前
2026-10-01~02 hetao1733837 的刷题记录
c++·算法
郝学胜-神的一滴10 小时前
AI 编程智能体 03:拆解当下主流智能体能力
开发语言·c++·人工智能·python·程序人生·游戏
宵时待雨11 小时前
linux笔记归纳25:多路转接epoll
linux·服务器·网络·c++
Frank_refuel11 小时前
【C++项目】手撕JRpc框架(单机怎么优化到分布式思想)
开发语言·c++