简体   繁体   English

如何通过主线程中的名称挂起线程?

[英]How to suspend a thread by its name from the main thread?

I have a local thread which is started on button click event from the main thread.Its name is 我有一个本地线程,它从主线程的按钮点击事件开始。它的名字是

"RegistrationThread".Now I want to stop this thread on another stop button click event.How do “RegistrationThread”。现在我想在另一个停止按钮点击事件上停止这个线程。怎么做

I stop "RegistrationThread" which is a local thread and i can not access this thread by its 我停止“RegistrationThread”这是一个本地线程,我不能通过它访问此线程

name. 名称。 If I get the "RegistrationThread" then it is possible to suspend it. 如果我得到“RegistrationThread”,则可以暂停它。 How to do this? 这个怎么做?

As far as I know, there's no nice way to find a thread by name, and Thread.Name is basically indended only to make debugging easier. 据我所知,没有很好的方法可以按名称查找线程,而Thread.Name基本上只是为了使调试更容易。 Why not just hold onto a reference to the thread object instead? 为什么不直接保持对线程对象的引用?

It is possible to both stop (abort) and suspend another thread in .net, however neither are particularly advisable things to do. 可以在.net中停止(中止)和挂起另一个线程,但是两者都不是特别可取的事情。

It's generally considered bad form to call Thread.Abort. 调用Thread.Abort通常被认为是不好的形式。 It's much safer to inform the code running in the other thread that it's time to abort and let it exit on its own at a safe point. 通知在另一个线程中运行的代码更加安全,它是时候中止并让它在安全点自行退出。 Consider that the MSDN documentation only claims that calling Abort() on a thread "Usually" aborts the thread. 考虑到MSDN文档仅声称在线程“通常”上调用Abort()会中止该线程。 This should be a sign that the API is really not meant to be used when results matter. 这应该表明,当结果很重要时,API实际上并不适合使用。

Suspending a thread is at least as evil as aborting it--if it's holding locks, suspending it could result in a deadlock. 暂停一个线程至少和中止它一样邪恶 - 如果它持有锁,挂起它可能会导致死锁。 Even if your code contains no explicit locks, the .net runtime has many behind the scenes (eg for class initialization) that could get you in trouble. 即使您的代码不包含显式锁,因此.net运行时在幕后有许多(例如,用于类初始化)可能会让您遇到麻烦。 As a result, microsoft deprecated Thread.Suspend years ago. 因此,微软几年前就弃用了Thread.Suspend。

Why don't you give us more information about what you're trying to do at a higher level and maybe we can help you find a better way. 为什么不向我们提供更多关于您在更高层次上尝试做什么的信息,也许我们可以帮助您找到更好的方法。

Aborting a thread is usually a measure of last resort. 中止线程通常是最后的手段。 The reason is because it can cause a lot of unintended side-effects. 原因是它可能导致许多意想不到的副作用。 The best practice is to allow the worker thread to end gracefully at predetermined safe points. 最佳实践是允许工作线程在预定的安全点优雅地结束。 This can be done correctly in many different ways. 这可以通过许多不同的方式正确完成。 One of the simplest is to set a flag indicating to the worker thread that it should begin shutting down. 最简单的方法之一是设置一个标志,指示工作线程应该开始关闭。

public class YourForm : Form
{
  private volatile bool m_Stop = false;

  private void StartButton_Click(object sender, EventArgs e)
  {
    var thread = new Thread(
      () =>
      {
        while (...)
        {
          // Periodically poll the m_Stop flag.
          if (m_Stop)
          {
            break;
          }
        }
      });
    thread.Start();
  }

  private void StopButton_Click(object sender, EventArgs e)
  {
    m_Stop = true;
  }
}

If you really want to abort a thread then you could always save away a reference to the Thread and then call Abort on it from your stop button click event. 如果你真的想要中止一个线程,那么你总是可以保存对Thread的引用,然后从你的停止按钮点击事件中调用Abort

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

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