在C ++中重新创建/重新打开线程 [英] re-creating/re-opening thread in C++

查看:240
本文介绍了在C ++中重新创建/重新打开线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在实现一个MFC S / W,它有两个按钮来创建一个线程来记录数据并取消它。

当一个按钮开始点击它,它应该创建一个线程

Hello
I'm implementing a MFC S/W and it has two buttons to create a thread to log data and cancel it.
When a button "start" is clicked, it supposed to create a thread by

hThread_log = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LogData_thr, this, NULL, &thrId_log);



LogData_thr函数while(1)在它中。

当点击一个按钮停止时,它应该停止线程。

我用CloseHandle()和一些标志变量尝试了TerminateThread()在函数LogData_thr中打破了无限循环。

但是,当我在停止线程后放置启动按钮(返回函数后)时,每次试验都会出现问题。

函数LogData_thr中发生了不必要的事情。

如何操作线程?

我总是感谢你给我的帮助。


The function LogData_thr has while(1) in it.
And when a button "stop" is clicked, it supposed to stop the thread.
I tried TerminateThread() with CloseHandle() and some flag variable which breaks the infinite loop in the function LogData_thr.
However, every trial caused a problem when I put "start" button after stopping the thread (after the function is returned).
Something unwanted happens inside the function LogData_thr.
How can I manipulate the thread?
I always appreciate your help you have given to me.

解决方案

有一个函数 TerminateThread 。我更喜欢设置一个全局变量,它被自己检查退出线程,所以它可以做一些清理。
There is the function TerminateThread for such issues. I like more setting a global variable which is checked for exiting the thread by its own, so it can do some cleanup.


这篇关于在C ++中重新创建/重新打开线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆