开发者

How long a thread is alive in c#

Is th开发者_Python百科ere any way to verify how long a Thread is alive?

I want to know how long my System.Threading.Thread.CurrentThread is alive in a ASP.NET page.


The thread will be alive until the IIS worker thread is recycled, or the server is restarted/turned off. The thread will be reused to handle more requests, so the lifetime of the thread can not be used to determine how long time it takes to handle a request (which I suspect that you really want to find out).

Besides, a single request can be handled by different threads. At certain points in the page life cycle, the execution can be taken over by another thread.

So, how long the thread has been alive is not relevant for the ASP.NET page.


AFAIK there is no direct method to achieve that...

BUT you could do this:

  • Get the ProcessThreadCollection of the current Process with Process.GetCurrentProcess().Threads
  • call GetCurrentThreadId (pinvoke since you need the native Thread ID!)
  • Iterate over this collection and find the current thread via ID
  • access the found ProcessThread.StartTime to get the Starttime of the current Thread

This gives you DateTime... which you would substract from DateTime.Now to get a TimeSpan... there you can access Ticks etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜