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);
}
}
}
Hello friends. I need to use pause process in my program and found this class on the net. But I do not know how to use it.
The processes that I will need to pause when I have PID are from the function below. Thanks for the help.
What I have tried:
<pre> private void button3_Click_1(object sender, EventArgs e)
{
try
{
BIT64.Text = "";
int pid = browser.Chrome();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "Google Chrome = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Opera();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "Opera = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Gameroom_Flash();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "FBGameroom = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Mustang();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "Mustang = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Firefox_Flash();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "Firefox_Flash = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Psafe();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "PsafeIternet = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Maxthon();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "Maxthon = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
pid = browser.Edge_Flash();
if (pid > 0)
{
Aobscan.OpenProcessid(pid);
label1.Text = "Microsoft = PID " + pid + "--" + BIT64.Text;
System.Media.SystemSounds.Beep.Play();
}
else
{
label1.Text = "OPS-Error Game open??";
System.Media.SystemSounds.Hand.Play();
Aobscan.processhandle = IntPtr.Zero;
}
}
}
I need to use the pause for the pid that is in the mount. It has a simple way of doing it. How can I call the function to pause. Sorry, I'm an apprentice.
UPDATE i use this class
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public static class Pause
{
[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);
}
}
public static void Print(this Process process)
{
Console.WriteLine("{0,8} {1}", process.Id, process.ProcessName);
}
public enum Options
{
List,
Kill,
Suspend,
Resume
}
[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)
}
public class Param
{
public int PId { get; set; }
public string Expression { get; set; }
public Options Option { get; set; }
}
}
and use like this
private void button5_Click(object sender, EventArgs e)
{
foreach (var process in Process.GetProcessesByName("firefox"))
process.Suspend();
}
But I need it to pause the process I've already done. Pid How do I do this?
That way I have to always find the new process to do.
Thank you.