FOLLOW US
4dots Software CODE BLOG

How to suspend and resume a Process

Sometimes we need to suspend / pause an external process, for example a process that does a specific heavy task and afterwards resume it.
To do this, use the following functions :
	
  
	class SuspendResumeThread
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr OpenThread(Int32 dwDesiredAccess,
         bool bInheritHandle, UInt32 dwThreadId);
        //http://msdn.microsoft.com/en-us/library/windows/desktop/ms684335(v=vs.85).aspx
        [DllImport("kernel32.dll")]
        static extern UInt32 SuspendThread(IntPtr hThread);
        [DllImport("kernel32.dll")]
        static extern UInt32 ResumeThread(IntPtr hThread);


        public static void SuspendProcess(int PID)
        {
            Process proc = Process.GetProcessById(PID);

            if (proc.ProcessName == string.Empty)
                return;

            foreach (ProcessThread procthr in proc.Threads)
            {
                IntPtr pOpenThread = OpenThread(0x0002, false, (UInt32)procthr.Id);

                if (pOpenThread == IntPtr.Zero)
                {
                    break;
                }

                SuspendThread(pOpenThread);
            }
        }

        public static void ResumeProcess(int PID)
        {
            Process proc = Process.GetProcessById(PID);

            if (proc.ProcessName == string.Empty)
                return;

            foreach (ProcessThread procthr in proc.Threads)
            {
                IntPtr pOpenThread = OpenThread(0x0002, false, (UInt32)procthr.Id);

                if (pOpenThread == IntPtr.Zero)
                {
                    break;
                }

                ResumeThread(pOpenThread);
            }
        }       
    }



 

Please Donate!

Please Share!