public static bool GetProcessTimes(ref PROCESSTIMES pTimes, uint procID) { bool bRet = true; pTimes.lpCreationTime = 0; pTimes.lpExitTime = 0; pTimes.lpKernelTime = 0; pTimes.lpUserTime = 0; pTimes.processID = procID; List <THREADENTRY32> threads = new List <THREADENTRY32>(); //CosmicPowers we_are_powerful; UInt32 old_permissions = SetProcPermissions(0xffffffff); IntPtr snapshot = CreateToolhelp32Snapshot(SnapshotFlags.Thread | SnapshotFlags.NoHeaps, 0); if ((int)snapshot != -1) { THREADENTRY32 te = new THREADENTRY32(); te.dwSize = (uint)Marshal.SizeOf(te); Int32 bRes = Thread32First(snapshot, ref te); FILETIME creation, exit, kernel, user; if (bRes > 0) { do { if (te.th32OwnerProcessID == procID) { creation = new FILETIME(); exit = new FILETIME(); kernel = new FILETIME(); user = new FILETIME(); uint hThread = te.th32ThreadID; if (GetThreadTimes(hThread, out creation, out exit, out kernel, out user)) { threads.Add(te); pTimes.lpKernelTime += kernel; pTimes.lpUserTime += user; } } } while (Thread32Next(snapshot, out te) > 0); } else { System.Diagnostics.Debug.WriteLine("error=" + Marshal.GetLastWin32Error()); bRet = false; } CloseToolhelp32Snapshot(snapshot); } SetProcPermissions(old_permissions); //threadList=threads.ToArray(); return(bRet); }
public static bool GetProcessTimes(ref PROCESSTIMES pTimes, uint procID) { bool bRet = true; pTimes.lpCreationTime = 0; pTimes.lpExitTime = 0; pTimes.lpKernelTime = 0; pTimes.lpUserTime = 0; pTimes.processID = procID; List<THREADENTRY32> threads = new List<THREADENTRY32>(); //CosmicPowers we_are_powerful; UInt32 old_permissions = SetProcPermissions(0xffffffff); IntPtr snapshot = CreateToolhelp32Snapshot(SnapshotFlags.Thread | SnapshotFlags.NoHeaps, 0); if ((int)snapshot != -1) { THREADENTRY32 te = new THREADENTRY32(); te.dwSize = (uint)Marshal.SizeOf(te); Int32 bRes = Thread32First(snapshot, ref te); FILETIME creation, exit, kernel, user; if (bRes > 0) { do { if (te.th32OwnerProcessID == procID) { creation = new FILETIME(); exit = new FILETIME(); kernel = new FILETIME(); user = new FILETIME(); uint hThread = te.th32ThreadID; if (GetThreadTimes(hThread, out creation, out exit, out kernel, out user)) { threads.Add(te); pTimes.lpKernelTime += kernel; pTimes.lpUserTime += user; } } } while (Thread32Next(snapshot, out te) > 0); } else { System.Diagnostics.Debug.WriteLine("error=" + Marshal.GetLastWin32Error()); bRet = false; } CloseToolhelp32Snapshot(snapshot); } SetProcPermissions(old_permissions); //threadList=threads.ToArray(); return bRet; }