如何获取当前线程的PrivilegedProcessorTime和UserProcessorTime

时间:2010-06-24 18:29:32

标签: c#-4.0 threadpool

我找到了几个如何为进程和所有线程获取PrivilegedProcessorTime和UserProcessorTime的示例,但是如何获取当前托管线程的PrivilegedProcessorTime和UserProcessorTime。

private void button1_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(WorkCore));
}

static void WorkCore(Object stateInfo)
{
    // Code to get and do work
    // Code to get and log PrivilegedProcessorTime and UserProcessorTime
}

1 个答案:

答案 0 :(得分:0)

我让这个工作,但AppDomain.GetCurrentThreadId已被弃用

static void WorkCore(Object stateInfo) 
{ 
    // Code to get and do work 
    // Code to get and log PrivilegedProcessorTime and UserProcessorTime
    ProcessThreadCollection m_Threads = Process.GetCurrentProcess().Threads;
    foreach(ProcessThread t in m_Threads)
    {
       if (t.Id == AppDomain.GetCurrentThreadId())
       {
          sw.WriteLine("User Time " + t.UserProcessorTime.ToString(@"d\:hh\:mm\:ss"));
          sw.WriteLine("Kernel Time " + t.PrivilegedProcessorTime.TotalSeconds.ToString("F0") + " sec");
       }
    }
}