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

运行程序

相关推荐
所以遗憾是什么呢?3 小时前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
Lhan.zzZ5 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
sysu_chenxy6 小时前
二重循环输出二维图形
c++
划破黑暗的第一缕曙光8 小时前
[C++]:2.类和对象(下)
c++·类和对象·对象·类
黎相思9 小时前
LRU Cache
c++·缓存
UIU1149 小时前
P1003 [NOIP 2011 提高组] 铺地毯
数据结构·c++·算法
xxwl58511 小时前
区域赛算法模版第四章---数据结构
数据结构·c++·算法
余额瞒着我当琳12 小时前
哈希表底层原理解析,直接定址法,哈希冲突,负载因子,哈希函数,开放定址法
java·c++·哈希算法·散列表
xxwl58513 小时前
区域赛算法模版第五章---数学与数论
c++·笔记·算法
局部有解13 小时前
LeetCode Hot100 438 找到字符串中所有字母异位词
c++·算法·leetcode