How to know who kills my threads

后端 未结 15 700
南旧
南旧 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:01

    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.

提交回复
热议问题