views:

66

answers:

1

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
}
A: 

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