-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Management; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace OKEGui.Task | ||
{ | ||
public class PauseResumeService | ||
{ | ||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); | ||
|
||
[Flags] | ||
public enum ThreadAccess : int | ||
{ | ||
TERMINATE = (0x0001), | ||
SUSPEND_RESUME = (0x0002), | ||
GET_CONTEXT = (0x0008), | ||
SET_CONTEXT = (0x0010), | ||
SET_INFORMATION = (0x0020), | ||
QUERY_INFORMATION = (0x0040), | ||
SET_THREAD_TOKEN = (0x0080), | ||
IMPERSONATE = (0x0100), | ||
DIRECT_IMPERSONATION = (0x0200) | ||
} | ||
|
||
[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); | ||
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)] | ||
static extern bool CloseHandle(IntPtr handle); | ||
|
||
|
||
private static void SuspendProcess(Process process) | ||
{ | ||
foreach (ProcessThread pT in process.Threads) | ||
{ | ||
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id); | ||
|
||
if (pOpenThread == IntPtr.Zero) | ||
{ | ||
continue; | ||
} | ||
|
||
SuspendThread(pOpenThread); | ||
CloseHandle(pOpenThread); | ||
} | ||
} | ||
|
||
private static void ResumeProcess(Process process) | ||
{ | ||
foreach (ProcessThread pT in process.Threads) | ||
{ | ||
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id); | ||
|
||
if (pOpenThread == IntPtr.Zero) | ||
{ | ||
continue; | ||
} | ||
|
||
int suspendCount = 2; | ||
while (suspendCount > 1) | ||
{ | ||
suspendCount = ResumeThread(pOpenThread); | ||
} | ||
|
||
CloseHandle(pOpenThread); | ||
} | ||
} | ||
|
||
private static List<Process> getChildProcesses(Process process) | ||
{ | ||
ManagementObjectSearcher searcher = new ManagementObjectSearcher( | ||
"SELECT *" + | ||
" FROM Win32_Process" + | ||
" WHERE ParentProcessId=" + process.Id.ToString()); | ||
ManagementObjectCollection collection = searcher.Get(); | ||
List<Process> res = new List<Process>(); | ||
if (collection.Count > 0) | ||
{ | ||
foreach (ManagementBaseObject item in collection) | ||
{ | ||
UInt32 childProcessId = (UInt32)item["ProcessId"]; | ||
if ((int)childProcessId != Process.GetCurrentProcess().Id) | ||
{ | ||
Process childProcess = Process.GetProcessById((int)childProcessId); | ||
res.Add(childProcess); | ||
res.AddRange(getChildProcesses(childProcess)); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
public static void PauseAll() | ||
{ | ||
List<Process> allProcesses = getChildProcesses(Process.GetCurrentProcess()); | ||
foreach (Process i in allProcesses) | ||
{ | ||
SuspendProcess(i); | ||
} | ||
} | ||
|
||
public static void ResumeAll() | ||
{ | ||
List<Process> allProcesses = getChildProcesses(Process.GetCurrentProcess()); | ||
foreach (Process i in allProcesses) | ||
{ | ||
ResumeProcess(i); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters