How to know who kills my threads

后端 未结 15 695
南旧
南旧 2020-12-05 00:19

I got a thread that is just banishing.. i\'d like to know who is killing my thread and why.

It occurs to me my thread is being killed by the OS, but i\'d like to

相关标签:
15条回答
  • 2020-12-05 01:03

    When you call RunWorker(), you can add a reference to your thread to a list. Once you have detected that your thread has died, you can inspect the state of the thread, perhaps it will reveal how it died. Or, perhaps it hasn't died, its just waiting on some resource (like the connection to the database).

    List runningThreads = ...
    public void RunWorker() {
        Thread worker = new Thread(delegate()
        ..
        runningThreads.add(worker);
        worker.Start();
    }
    
    public void checkThreads() {
     for (Thread t : runningThreads) {
       Console.WriteLine("ThreadState: {0}", t.ThreadState);
     }
    }
    
    0 讨论(0)
  • 2020-12-05 01:08

    Various people (including myself, here) pointed out that hosting a long-running thread in IIS is a bad idea. Your thread will being running inside an IIS 'worker process'. These processes are periodically terminated (recycled) by IIS, which will cause your thread to die.

    I suggest that you try turning-off IIS worker process recycling to see if that makes a difference. You can find more information here.

    0 讨论(0)
  • 2020-12-05 01:12

    A simple answer would be: "The killer doesn't leave a name card" ;)

    • If your thread is hosted in IIS, probably the thread is killed by the app pool process which recycles. The server might continue running but the process which hosts your item is stopped untill a new request fires everything up again.
    • If your thread is hosted in an executable, the only way it can be killed is by killing the thread yourself, throwing an exception in the thread or terminating the host process

    Hope this helps.

    0 讨论(0)
提交回复
热议问题