/// <summary> /// Gets the set of threads that are running in the associated process. /// </summary> /// <param name="p">The Process.</param> /// <returns>An array of type <see cref="ProcessThread"/> representing the operating system threads currently running in the associated process.</returns> public static System.Collections.ObjectModel.ReadOnlyCollection <ProcessThread> GetThreads(this Process p) { List <ProcessThread> threads = new List <ProcessThread>(); IntPtr handle = NativeMethods.CreateToolhelp32Snapshot(NativeMethods.TH32CS.SNAPTHREAD, p.Id); if (handle != IntPtr.Zero) { NativeMethods.THREADENTRY32 te = new NativeMethods.THREADENTRY32(); te.dwSize = Marshal.SizeOf(te); bool success = NativeMethods.Thread32First(handle, ref te); while (success) { if (te.th32CurrentProcessID == p.Id) { threads.Add(new ProcessThread(te)); } success = NativeMethods.Thread32Next(handle, ref te); } NativeMethods.CloseToolhelp32Snapshot(handle); } return(new System.Collections.ObjectModel.ReadOnlyCollection <ProcessThread>(threads)); }
internal ProcessThread(NativeMethods.THREADENTRY32 te) { this.te = te; }