[C#] {REQUEST} Suspend all processes.

01/15/2013 10:51 iCraziE#1
Hi, I'm looking for a way to suspend all processes with the same name.

EDIT: Sorry I posted too soon, I found a solution.
01/15/2013 12:31 »jD«#2
Just for those who are looking for this in the future. I use this (credits goto stackoverflow):

Code:
public static class ProcessExtension
{
    [DllImport("kernel32.dll")]
    static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
    [DllImport("kernel32.dll")]
    static extern uint SuspendThread(IntPtr hThread);
    [DllImport("kernel32.dll")]
    static extern int ResumeThread(IntPtr hThread);

    public static void Suspend(this Process process)
    {
        foreach (ProcessThread thread in process.Threads)
        {
            var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
            if (pOpenThread == IntPtr.Zero)
            {
                break;
            }
            SuspendThread(pOpenThread);
        }
    }
    public static void Resume(this Process process)
    {
        foreach (ProcessThread thread in process.Threads)
        {
            var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
            if (pOpenThread == IntPtr.Zero)
            {
                break;
            }
            ResumeThread(pOpenThread);
        }
    }
}
You can then use it as an extension method the the Process class.

Code:
foreach(var process in Process.GetProcessesByName("notepad"))
    process.Suspend();
-jD