wxWidgets wxThread 删除可连接线程

发布于 2024-11-03 10:00:30 字数 226 浏览 0 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺ぐ爱和你が 2024-11-10 10:00:30

wxThread::Delete() 的当前文档说:

此函数在可连接线程上工作,但在这种情况下,线程的 TestDestroy() 函数返回 true,然后等待其完成(即,它与 Wait() 不同,因为它要求线程在等待之前终止)。

因此,您似乎可以在可连接线程上使用Delete()

The current documentation for wxThread::Delete() says:

This function works on a joinable thread but in that case makes the TestDestroy() function of the thread return true and then waits for its completion (i.e. it differs from Wait() because it asks the thread to terminate before waiting).

So, it appears that you can use Delete() on a joinable thread.

还如梦归 2024-11-10 10:00:30

您必须从工作线程调用 Exit() 方法,或者简单地从 Run 方法返回并调用 MyThread->Wait() 方法,然后删除线程对象。

声明线程:

class MyThread : public wxThread {
  virtual void * run();
};

线程实现:

MyThread::run()
{
  while(1)
  {
    if(TestDestroy())
    {
        this.Exit();  // or return;
    }
    // Do some work
  }
}

声明线程指针:

MyThread * pMyThread;

创建、启动和停止线程

void launchThread{
  pMyThread = new wxThread(wxTHREAD_JOINABLE);
  pMyThread->Create();
  pMyThread->Run();
}

void stopThread(){
  pMyThread->Delete();
  pMyThread->Wait();
  delete pMyThread;
}

希望它有所帮助。

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 :

class MyThread : public wxThread {
  virtual void * run();
};

Thread implementation :

MyThread::run()
{
  while(1)
  {
    if(TestDestroy())
    {
        this.Exit();  // or return;
    }
    // Do some work
  }
}

Declaring the Thread pointer :

MyThread * pMyThread;

Creating, launching and stopping the thread

void launchThread{
  pMyThread = new wxThread(wxTHREAD_JOINABLE);
  pMyThread->Create();
  pMyThread->Run();
}

void stopThread(){
  pMyThread->Delete();
  pMyThread->Wait();
  delete pMyThread;
}

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?

风筝在阴天搁浅。 2024-11-10 10:00:30

您不必重写代码。

正如文档所建议的那样,线程通常最好通过从其主函数返回来终止。

实现此目的的一种方法(可能也是最简单的方法)是抛出一些将在主线程函数中捕获的对象。

例如:

struct ThreadEndingException { };

void DoSomeWork() {
    ...
    if (TestDestroy())
        throw ThreadEndingException();
    ...
}

void ThreadFunction() {
    try {
        DoSomeWork();
    }
    catch (const ThreadEndingException&) {
        // Do nothing, the function will return after leaving this catch.
    }
}

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:

struct ThreadEndingException { };

void DoSomeWork() {
    ...
    if (TestDestroy())
        throw ThreadEndingException();
    ...
}

void ThreadFunction() {
    try {
        DoSomeWork();
    }
    catch (const ThreadEndingException&) {
        // Do nothing, the function will return after leaving this catch.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文