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 vo开发者_如何学JAVAid WorkCore(Object stateInfo)
{
// Code to get and do work
// Code to get and log PrivilegedProcessorTime and UserProcessorTime
}
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");
}
}
}
精彩评论