///// Time a thread has spent working //public struct thread_times //{ // /// Time a thread has spent in kernel space // public FILETIME kernel; // /// Time a thread has spent in user space // public FILETIME user; //}; /// <summary> /// return the sum of all thread times of this process /// </summary> /// <param name="processID"></param> /// <param name="lpKernelTime"></param> /// <param name="lpUserTime"></param> /// <returns></returns> public static int GetProcessTimes(uint processID, ref FILETIME lpKernelTime, ref FILETIME lpUserTime, ref long duration) { //threadStruct[] myThreads = new threadStruct[255];//hopefully enough int iCount = 0; uint startTicks = 0;//, duration=0; long FTprocKernel = 0, FTprocUser = 0, FTprocTotal = 0; CosmicPowers cosmicPower = new CosmicPowers(); IntPtr snapshot = CreateToolhelp32Snapshot(SnapshotFlags.Thread | SnapshotFlags.NoHeaps, 0); if ((int)snapshot != INVALID_HANDLE_VALUE) { THREADENTRY32 te = new THREADENTRY32(); te.dwSize = (uint)Marshal.SizeOf(te); startTicks = GetTickCount(); Int32 bRes = Thread32First(snapshot, ref te); if (bRes > 0) { do { FILETIME creation = new FILETIME(); FILETIME exit = new FILETIME(); FILETIME kernel = new FILETIME(); FILETIME user = new FILETIME(); uint hThread = te.th32ThreadID; if (te.th32OwnerProcessID == processID) { if (GetThreadTimes(hThread, out creation, out exit, out kernel, out user)) { //add the thread's values to our sum FTprocKernel += kernel; FTprocUser += user; FTprocTotal += kernel + user; //System.Diagnostics.Debug.WriteLine( // te.th32OwnerProcessID.ToString() + ": \t" + // kernel.ToString() + "\t" + // user.ToString()); iCount++; } } } while (Thread32Next(snapshot, out te) > 0); } else { System.Diagnostics.Debug.WriteLine("error=" + Marshal.GetLastWin32Error()); } CloseToolhelp32Snapshot(snapshot); } cosmicPower.Dispose(); lpKernelTime = FTprocKernel; lpUserTime = FTprocUser; //System.Diagnostics.Debug.WriteLine("usage: " + ((float)(((FTprocKernelDuration+FTprocUserDuration) / 100f) * duration)).ToString("0.00%")); return(iCount); }
///// Time a thread has spent working //public struct thread_times //{ // /// Time a thread has spent in kernel space // public FILETIME kernel; // /// Time a thread has spent in user space // public FILETIME user; //}; /// <summary> /// return the sum of all thread times of this process /// </summary> /// <param name="processID"></param> /// <param name="lpKernelTime"></param> /// <param name="lpUserTime"></param> /// <returns></returns> public static int GetProcessTimes(uint processID, ref FILETIME lpKernelTime, ref FILETIME lpUserTime, ref long duration) { //threadStruct[] myThreads = new threadStruct[255];//hopefully enough int iCount = 0; uint startTicks = 0;//, duration=0; long FTprocKernel = 0, FTprocUser = 0, FTprocTotal = 0; CosmicPowers cosmicPower = new CosmicPowers(); IntPtr snapshot = CreateToolhelp32Snapshot(SnapshotFlags.Thread | SnapshotFlags.NoHeaps, 0); if ((int)snapshot != INVALID_HANDLE_VALUE) { THREADENTRY32 te = new THREADENTRY32(); te.dwSize = (uint)Marshal.SizeOf(te); startTicks = GetTickCount(); Int32 bRes = Thread32First(snapshot, ref te); if (bRes > 0) { do { FILETIME creation = new FILETIME(); FILETIME exit = new FILETIME(); FILETIME kernel = new FILETIME(); FILETIME user = new FILETIME(); uint hThread = te.th32ThreadID; if (te.th32OwnerProcessID == processID) { if (GetThreadTimes(hThread, out creation, out exit, out kernel, out user)) { //add the thread's values to our sum FTprocKernel += kernel; FTprocUser += user; FTprocTotal += kernel + user; //System.Diagnostics.Debug.WriteLine( // te.th32OwnerProcessID.ToString() + ": \t" + // kernel.ToString() + "\t" + // user.ToString()); iCount++; } } } while (Thread32Next(snapshot, out te) > 0); } else { System.Diagnostics.Debug.WriteLine("error=" + Marshal.GetLastWin32Error()); } CloseToolhelp32Snapshot(snapshot); } cosmicPower.Dispose(); lpKernelTime = FTprocKernel; lpUserTime = FTprocUser; //System.Diagnostics.Debug.WriteLine("usage: " + ((float)(((FTprocKernelDuration+FTprocUserDuration) / 100f) * duration)).ToString("0.00%")); return iCount; }