以最安全的方式循环循环C#检测线程是否还活着

发布于 2025-02-06 22:33:45 字数 1252 浏览 2 评论 0原文

我试图在线程内结束线程,当中止时,我想检测到该线程已被中止(或者真正停止),

这就是我要做的事情,但是由于它永远不会起作用,因此无法正常工作在这一部分中,

if (!thread.IsAlive){
   CommandPrompt.SayMessage("Oops! MBP thread died.");
}

这就是我要做的事情,谢谢您的帮助。抱歉,如果我的问题令人困惑,请提出问题,以便我可以帮助您帮助我:)谢谢!

public static Thread thread;
        public static void 1()
        {
            thread = new Thread(thread1);
            thread.Start();
            if (!thread.IsAlive)
            {
                CommandPrompt.SayMessage("Oops! MBP thread died.");
            }
        }
        public static void thread1()
        {
            {
             int test = 0;
             while (thread.IsAlive){
                 Console.WriteLine("running.."); // this text will be displayed when the thread is active, and will stop when stopped.
                 Thread.Sleep(2500);
                 test += 1;
                 if (test > 4) // after 4 loops, i want the thread to end itself
                     thread.Abort();
             }
             if (!thread.IsAlive){ // once the thread is ended i want it to tell us that, but it never gets to this part.
                 CommandPrompt.SayMessage("Oops! MBP thread died.");
             }

            }
        }

I am trying to end my Thread, within the thread, and when aborted, i want to detect that the thread has been aborted (or just, stopped really)

This is what i am doing to do that, but isnt working as it never gets to this part

if (!thread.IsAlive){
   CommandPrompt.SayMessage("Oops! MBP thread died.");
}

This is how i'm going about it, thank you for the help. sorry if my question is confusing please ask questions so i can help you help me :) thanks!

public static Thread thread;
        public static void 1()
        {
            thread = new Thread(thread1);
            thread.Start();
            if (!thread.IsAlive)
            {
                CommandPrompt.SayMessage("Oops! MBP thread died.");
            }
        }
        public static void thread1()
        {
            {
             int test = 0;
             while (thread.IsAlive){
                 Console.WriteLine("running.."); // this text will be displayed when the thread is active, and will stop when stopped.
                 Thread.Sleep(2500);
                 test += 1;
                 if (test > 4) // after 4 loops, i want the thread to end itself
                     thread.Abort();
             }
             if (!thread.IsAlive){ // once the thread is ended i want it to tell us that, but it never gets to this part.
                 CommandPrompt.SayMessage("Oops! MBP thread died.");
             }

            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

可爱暴击 2025-02-13 22:33:45

如果要检查线程是否还活着,则无法从该特定线程中运行的代码执行此操作。因为,如果您可以执行代码以检查线程是否还活着,那么显然是还活着的。如果线程不再活着,显然将无法执行更多代码...

因此,唯一的方法是从线程外部的某个地方进行此操作(即其他一些线程,甚至可能是主线程)。

只是示意性的代码,这很笨拙,但会给您第一个了解如何解决此问题的第一想法。但是,如果有第三方处理您的线程,那将无济于事,因为什么阻止了它们杀死您的整个应用程序?

public class ThreadTest {

  static bool ranToEnd = false;

  public static void Main(){
    var thread = new Thread(aThread);
    var lc = 1;
    thread.Start();

    while (true){  
      if (!aThread.IsAlive) {
        if (ranToEnd)
          Console.WriteLine("aThread terminated normally");
        else
          Console.WriteLine("aThread ended prematurely");

        break;
      } else if (++lc == 10) {
        aThread.Abort(); //Simulating the abortion of the thread
      }
      Thread.Sleep(1000);
    }
  }

  public static void aThread() {
    //do some work in this thread

    // if the thread ran to an end normally, this will be set to true
    // if the thread ended prematurely, this will stay false ... 
    ranToEnd = true;
  }

}

If you want to check if a thread is still alive, you cannot do that from the code running in that particular thread. Because, if you can execute the code to check if the thread is still alive, it is -- obviously -- still alive. And if the thread is not alive anymore it will -- obviously -- not be able to execute any more code ...

So the only way is to do this from somewhere outside the thread (ie some other thread, maybe even the main thread).

Just schematic code, which is rather clumsy, but will give you a first idea of how you can address this issue. But if there is a third party messing around with your threads, that won't help anything, because what prevents them from killing off your whole application?

public class ThreadTest {

  static bool ranToEnd = false;

  public static void Main(){
    var thread = new Thread(aThread);
    var lc = 1;
    thread.Start();

    while (true){  
      if (!aThread.IsAlive) {
        if (ranToEnd)
          Console.WriteLine("aThread terminated normally");
        else
          Console.WriteLine("aThread ended prematurely");

        break;
      } else if (++lc == 10) {
        aThread.Abort(); //Simulating the abortion of the thread
      }
      Thread.Sleep(1000);
    }
  }

  public static void aThread() {
    //do some work in this thread

    // if the thread ran to an end normally, this will be set to true
    // if the thread ended prematurely, this will stay false ... 
    ranToEnd = true;
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文