Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know who kills my threads

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 confirm this and if possible to know why it's killing it.

As for the thread, i can assert it has at least 40 min of execution before dying, but it suddenly dies around 5 min.

public void RunWorker() {     Thread worker = new Thread(delegate()     {         try         {             DoSomethingForALongLongTime();         }         catch(Exception e)         {            //Nothing is never logged :(            LogException(e);            throw e;         }     });      worker.IsBackground = true;     worker.SetApartmentState(System.Threading.ApartmentState.STA);     worker.Start(); } 

EDIT: Addressing answers

  • Try/Catch Possible exceptions:
    It's implemented and it catches nothing :(
  • Main Thread dying:
    This thread is created by the web server, which continues to run
  • Work completion:
    The work is not completed, as it finally affects the database, i can check whether it's done or not when the thread dies.

Having thought of these things brought me to this question, who is killing my threads??

ps. It's not Lady Goldent in the living room with the candle stick :)

like image 509
mcabral Avatar asked Apr 20 '10 14:04

mcabral


People also ask

Does killing a process kill all threads?

When you kill process, everything that process owns, including threads is also killed. The Terminated property is irrelevant. The system just kills everything.

How do you know when a thread has finished?

Use Thread. Join(TimeSpan. Zero) It will not block the caller and returns a value indicating whether the thread has completed its work.

How do I kill a specific thread?

Modern ways to suspend/stop a thread are by using a boolean flag and Thread. interrupt() method. Using a boolean flag: We can define a boolean variable which is used for stopping/killing threads say 'exit'. Whenever we want to stop a thread, the 'exit' variable will be set to true.


2 Answers

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.

like image 53
Andy Johnson Avatar answered Oct 05 '22 22:10

Andy Johnson


Your thread probably just threw an exception. Try putting a try/catch block around DoSomethingForALongLongTime and see what it picks up.


Update: I didn't notice before that you were starting this from a web server. That can be a very bad idea. In particular, is the separate thread using any information derived from HttpContext.Current? That would include Request, Response, Session, etc., as well as any information from the page.

This is bad because these things only last as long as the request lasts. Once the request is over, they become invalid, to say the very least.

If you need to kick off a long-running thread from within a web application or web service, then you should create a simple Windows Service and host a WCF service within it. Have the web page then send all the information needed to perform the task to the service. The service can even use MSMQ as a transport, which will ensure that no messages are lost, even if the service gets busy.

like image 25
John Saunders Avatar answered Oct 05 '22 23:10

John Saunders