C#如何暂停进程PID [英] C# how pause process PID

查看:83
本文介绍了C#如何暂停进程PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 暂停(处理流程)
{
foreach (ProcessThread线程) in process.Threads)
{
var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME) , false ,( uint )thread.Id);
if (pOpenThread == IntPtr .Zero)
{
break ;
}
SuspendThread(pOpenThread);
}
}
public static void 恢复(处理流程)
{
foreach (ProcessThread线程 in process.Threads)
{
var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false ,( uint )thread.Id);
if (pOpenThread == IntPtr .Zero)
{
break ;
}
ResumeThread(pOpenThread);
}
}
}







朋友你好。我需要在我的程序中使用暂停过程并在网上找到这个类。但是我不知道如何使用它。

当我有PID时我需要暂停的过程来自下面的函数。感谢您的帮助。



我的尝试:



< pre lang =c#>< 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-错误游戏打开??;
System.Media.SystemSounds.Hand.Play();
Aobscan.processhandle = IntPtr .Zero;

}
}
}







我需要使用挂载中的pid的暂停。它有一个简单的方法。如何调用该函数暂停。对不起,我是学徒。





更新我使用这个课程



使用System; 
使用System.Diagnostics;
使用System.Runtime.InteropServices;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;



公共静态类暂停

{




[ 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(此处理流程)
{
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打印(此处理流程)
{
Console.WriteLine({0,8} {1},process.Id ,process.ProcessName);
}


公共枚举选项
{
列表,
Kill,
暂停,
恢复
}

[标志]
公共枚举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)
}

公共类Param
{
public int PId {get;组; }
public string Expression {get;组; }
public Options Option {get;组; }
}
}





并使用这样的



  private   void  button5_Click( object  sender,EventArgs e)
{
foreach var 进程 Process.GetProcessesByName( firefox ))


process.Suspend();

}







但我需要暂停我已经完成的过程。 Pid我该怎么做?

那样我总能找到新的流程。



谢谢。

解决方案

我认为你不会喜欢它,但这是一种在C#中实现它的方法:.net - 在C#中暂停进程 - 堆栈溢出 [ ^ ]


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.

解决方案

You won't like it I think, but here is a way to do it in C#: .net - Suspend Process in C# - Stack Overflow[^]


这篇关于C#如何暂停进程PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆