wxWidgets wxThread 删除可连接线程
我有一个关于 wxWidgets 中可连接线程的问题。
当用户需要时,我想停止线程做一些工作。因此,我调用这个工作线程 TestDestroy() 来检查是否应该停止该线程。但我只能通过调用Delete() 以这种方式停止线程,对于可连接线程不应该调用Delete()。
我是否有可能停止线程(使用 TestDestroy)或者我是否必须完全更改我的代码?
提前致谢,
蒂博
I got a question about joinable threads in wxWidgets.
When the user wants it, I want to stop a thread doing some work. For that reason I call in this worker thread TestDestroy() to check whether the thread should be stopped. But I can only stop the thread this way by calling Delete(), which should not be called for joinable threads.
Is there a possibility for me to stop the thread (using TestDestroy) or do I have to change my code completely?
Thanks in advance,
TiBo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
wxThread::Delete() 的当前文档说:
因此,您似乎可以在可连接线程上使用
Delete()
。The current documentation for wxThread::Delete() says:
So, it appears that you can use
Delete()
on a joinable thread.您必须从工作线程调用 Exit() 方法,或者简单地从 Run 方法返回并调用 MyThread->Wait() 方法,然后删除线程对象。
声明线程:
线程实现:
声明线程指针:
创建、启动和停止线程
希望它有所帮助。
PS:这是我在 Stack Overflow 上的第一个回答。我不知道如何轻松编写一些自动缩进的代码?
You have to call the Exit() method from your worker thread or simply return from the Run method AND call the MyThread->Wait() method then delete the thread object.
Declaring the thread :
Thread implementation :
Declaring the Thread pointer :
Creating, launching and stopping the thread
Hope that it helps.
P.S. : this is my first answer on Stack Overflow. I don't know how I can easilly write some code automatically indented?
您不必重写代码。
正如文档所建议的那样,线程通常最好通过从其主函数返回来终止。
实现此目的的一种方法(可能也是最简单的方法)是抛出一些将在主线程函数中捕获的对象。
例如:
You shouldn't have to rewrite your code.
It's usually best that a thread terminates by returning from it's main function, as the documentation suggests.
One way of achieving this, and probably the easiest, is to throw some object that will be caught in the main thread function.
For example: