如何获取当前线程的ProcessThread.TotalProcessorTime
好的,我正在使用 ThreadPool 来启动线程。在线程代码内部,我试图找出它实际使用的 cpu 时间。我读过有 ProcessThread 。 TotalProcessorTime 属性(http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.totalprocessortime.aspx),但我无法阅读它。那么如何获取当前线程的它呢?
ok, I am using ThreadPool to start threads. Inside threaad code I'm trying to figure out hom much cpu time it actually used. I've read there is ProcessThread . TotalProcessorTime property for this (http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.totalprocessortime.aspx) but I just can't get to read it. So how do I get it for a current thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几点:
通过
ProcessThread.TotalProcessorTime
您需要知道本机线程 ID。听起来很像您正在将托管线程 ID(通过 Thread.CurrentThread.Name)与本机线程 ID 进行比较,以通过 System.Diagnostics 获取当前线程 命名空间。请记住,托管线程 ID != ProcessThreadID。您需要使用
GetCurrentThreadId
函数线程池线程可以回收,因此线程池的TotalProcessorTime可能会比你期望的要大
如果您正在分析线程使用情况,那么使用 Visual Studio Ultimate 中的线程分析器或其他好的分析器可能会更容易(例如ANTS、DotTrace、SciTech 等)
A few points:
To get the correct process thread via
ProcessThread.TotalProcessorTime
you'll need to know the native thread ID.It sounds very much like you are comparing the managed thread ID (via
Thread.CurrentThread.Name
) with the native thread ID to get the current thread within the via theSystem.Diagnostics
namespace. Remember that managed thread ID != ProcessThreadID. You'll need to use theGetCurrentThreadId
functionThread pool threads can be recycled, so the TotalProcessorTime for a thread pool may be larger than what you are expecting
If you are profiling thread usage it may be easier to use the thread profiler in Visual Studio Ultimate, or another good profiler (e.g. ANTS, DotTrace, SciTech, etc.)