简体   繁体   English

无法终止线程(错误6)

[英]Couldn't terminate thread (error 6)

We have a huge, complex wxWidgets application written in C++. 我们有一个庞大的,复杂的用C ++编写的wxWidgets应用程序。 I added an extra background thread. 我添加了一个额外的后台线程。 When the user clicks "go", the thread starts. 当用户单击“开始”时,线程启动。 When they click "stop", the thread stops. 当他们单击“停止”时,线程停止。 For reasons beyond my comprehension, clicking "stop" also causes the following message to be displayed: 由于超出我的理解范围,单击“停止” 还会导致显示以下消息:

Can not wait for thread termination (error 6: the handle is invalid.)
Couldn't terminate thread (error 6: the handle is invalid.)

Why the hell is this happening?? 为什么发生这种情况? And more importantly, how do I make this go away immediately? 更重要的是,我如何立即消除这种情况?

The thread is started here: 线程从这里开始:

_worker = new WorkerThread();
_worker->Create();
_worker->Run();

I know for a fact that the thread is running, because I can see the disk files it's writing. 我知道线程正在运行,因为我可以看到它正在写入的磁盘文件。

The thread is stopped here: 线程在这里停止:

if (_worker)
{
  _worker->Delete();
  _worker = NULL;
}

The WorkerThread class only overrides Enter() . WorkerThread类仅覆盖Enter() It is definitely a detachable thread. 绝对是可分离的线程。

The documentation is full of dire warnings about how a detachable thread can delete itself at any moment, and everything must always be wrapped in a critical section. 该文档充满了关于可拆卸线程如何随时删除自身的可怕警告,所有内容必须始终包裹在关键部分。 But my worker thread runs forever, until I tell it to stop. 但是我的工作线程会永远运行,直到我告诉它停止为止。 I can't see why I would need a critical section for anything. 我看不到为什么我需要为任何内容提供关键部分。

Is the thread taking too long to stop? 线程是否花了太长时间才能停止? Is that the problem? 是问题吗? (It only checks TestDestroy() once per second. Is that too slow?) (它每秒只检查一次TestDestroy() 。太慢了吗?)

I really can't figure out how the hell to solve this. 我真的不知道怎么解决这个问题。

You may "make it go away" by using wxLogNull , as with any other messages generated by wxWidgets. 可以使用wxLogNull “使它消失”,就像由wxWidgets生成的任何其他消息一样。 You should not do this however as you seem to have a real bug somewhere in your code, the thread handle obviously should not be invalid and if it is, something clearly doesn't go as you think it does. 但是,您应该这样做,因为您似乎在代码中的某个地方有一个真正的错误,线程句柄显然不应无效,如果确实如此,则某些东西显然不会按照您的想法运行。 By sweeping the error under the carpet you all but guarantee that it will reappear in a different guise at the worst possible moment and typically on a clients machine where you will be unable to debug it. 通过彻底清除错误,您几乎可以保证所有错误都会在最坏的情况下以其他伪装再次出现,并且通常会在无法调试它的客户端计算机上显示。 Better really do it now. 最好现在就做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM