为什么这种关闭工作线程的方法不起作用? [英] Why doesn't this method of closing a worker thread work?
问题描述
我有一个MFC应用程序,其GUI有一个开始和一个停止按钮。开始按钮定期调用我想在后台运行的功能。我的解决方案是使用 SetTimer
函数定期调用 OnTimer
函数。然后,此函数使用 AfxBeginThread
创建一个线程,然后填充我作为参数传递给线程的结构。所有这一切都很好。
我现在想做的是让停止按钮结束线程。我已经阅读了 TerminateThread
,但它看起来风险太大了。由于我想从另一个线程关闭工作线程,我知道我不能使用 AfxEndThread
,但我想知道为什么以下解决方案不起作用。 />
我尝试了什么:
我创建了 int
变量名为 count
并在按下start时将其设置为 0
并设置每当按下停止时它都 1
我传入的结构 AfxBeginThread
看起来像这样
I have an MFC application whose GUI has a start and a stop button. The start button calls a function which I want to run in the background, periodically. My solution was to use the SetTimer
function to call the OnTimer
function at regular intervals. This function then creates a thread using AfxBeginThread
and then populates the structure that I pass to the thread as a parameter. All this works just fine.
What I would like to do now is the make the stop button end the thread. I have read about TerminateThread
but it seems too risky. Since I want to close the worker thread from another thread, I know I can't use AfxEndThread
, but I wanted to know why the following solution doesn't work.
What I have tried:
I created an int
variable called count
and set it to 0
whenever start is pressed, and set it to 1
whenever stop is pressed.
The structure I am passing into AfxBeginThread
looks like this
typedef struct THREADSTRUCT
{
MyGUIDlg* _this;
int flag;
//you can add here other parameters you might be interested on
} THREADSTRUCT;
当开始时是按下 SetTimer
函数被调用,其回调函数定义如下:
When start is pressed the SetTimer
function is called, whose callback function is defined like this:
void MyGUIDlg::OnTimer(UINT_PTR nIDEvent)
{
THREADSTRUCT* _param = new THREADSTRUCT;
_param->_this=this;
_param->flag = count;
AfxBeginThread(StartThread, _param);
CDialog::OnTimer(nIDEvent);
}
在我的StartThread功能中,我添加了以下内容:
In my StartThread function I have added the following:
UINT MyGUIDlg::StartThread(LPVOID param)
{
THREADSTRUCT *ts = (THREADSTRUCT*)param;
if((ts->flag)==1)
{
//Use AfxEndThread to close this thread
}
return 0;
}
但这似乎不起作用,有什么想法?
But this doesn't seem to work, any thoughts why?
推荐答案
工作线程通常包含一个循环,用于检查停止条件:
A worker thread usually contains a loop where it checks for a stop condition:
UINT MyGUIDlg::StartThread(LPVOID param)
{
THREADSTRUCT *ts = (THREADSTRUCT*)param;
while (!ts->flag)
{
// Perform operations here
// May also break the loop for self termination
}
// Thread is closed when it returns or when
// AfxEndThread() is called.
// When m_bAutoDelete is TRUE, the thread object is deleted.
return 0;
}
上面轮询一个共享变量变量来检查线程是否应该终止。更好的解决方案是使用某种信号(事件)。
但是在你的例子中没有循环,线程总是立即关闭。
我建议阅读有关工作线程的信息。一篇陈旧但非常好的文章是使用工作线程 [ ^ ]。另请参见使用C ++和MFC进行多线程处理 [ ^ ]。
The above polls a shared variable variable to check if the thread should terminate. A better solution is using some kind of signals (events).
But in your example there is no loop and the thread is always closed immediately.
I suggest to read about worker threads. An old but very good article is Using Worker Threads[^]. See also Multithreading with C++ and MFC[^] in the MSDN.
这篇关于为什么这种关闭工作线程的方法不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!