[英]How long a thread is alive in c#
Is there any way to verify how long a Thread
is alive? 有什么方法可以验证一个
Thread
存活多长时间?
I want to know how long my System.Threading.Thread.CurrentThread
is alive in a ASP.NET page. 我想知道我的
System.Threading.Thread.CurrentThread
在ASP.NET页中存活了多长时间。
The thread will be alive until the IIS worker thread is recycled, or the server is restarted/turned off. 直到IIS工作线程被回收,或者服务器重新启动/关闭之前,该线程将一直处于活动状态。 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. 因此,线程存活的时间与ASP.NET页无关。
AFAIK there is no direct method to achieve that... AFAIK没有直接的方法可以实现...
BUT you could do this: 但是您可以这样做:
ProcessThreadCollection
of the current Process
with Process.GetCurrentProcess().Threads
Process.GetCurrentProcess().Threads
获取当前Process
的ProcessThreadCollection
GetCurrentThreadId
(pinvoke since you need the native Thread ID!) GetCurrentThreadId
(调用pinvoke,因为您需要本机线程ID!) ProcessThread.StartTime
to get the Starttime of the current Thread ProcessThread.StartTime
以获取当前线程的开始时间 This gives you DateTime
... which you would substract from DateTime.Now
to get a TimeSpan
... there you can access Ticks
etc. 这将为您提供
DateTime
...,您可以从DateTime.Now
减去它。现在可以获取TimeSpan
...在那里您可以访问Ticks
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.