Monday, February 21, 2011

How do I get the PrivilegedProcessorTime and UserProcessorTime for the current thread

I found several examples of how to get the PrivilegedProcessorTime and UserProcessorTime for a process and for all threads, but how do I get the PrivilegedProcessorTime and UserProcessorTime for the current managed thread.

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
}
From stackoverflow
  • I got this to work but AppDomain.GetCurrentThreadId has been deprecated

    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");
           }
        }
    } 
    

0 comments:

Post a Comment