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

运行程序

相关推荐
阿林学习计算机8 小时前
C++11特性
c++
Elias不吃糖8 小时前
NebulaChat:C++ 高并发聊天室服务端
开发语言·c++·redis·sql·项目文档
帅中的小灰灰8 小时前
C++编程策略设计模式
开发语言·c++·设计模式
是小胡嘛9 小时前
华为云CentOS系统中运行http服务器无响应
linux·服务器·c++·http·centos·华为云
福尔摩斯张10 小时前
C语言核心:string函数族处理与递归实战
c语言·开发语言·数据结构·c++·算法·c#
江澎涌10 小时前
JHandler——一套简单易用的 C++ 事件循环机制
android·c++·harmonyos
liu****10 小时前
5.C语言数组
c语言·开发语言·c++
毛甘木10 小时前
Unity MonoPInvokeCallback 使用教程
c++·unity
吗~喽11 小时前
【LeetCode】滑动窗口_水果成篮_C++
c++·算法·leetcode
BestOrNothing_201511 小时前
【C++基础】Day 4:关键字之 new、malloc、constexpr、const、extern及static
c++·八股文·static·extern·new与malloc·constexpr与const