C#process.start()暂停执行-BEGINNER

本文关键字:执行 -BEGINNER 暂停 start C#process | 更新日期: 2023-09-27 18:00:12

我有一个C#窗体应用程序,上面几乎没有按钮。我用Process.start执行了一个.exe程序(程序一启动就会出现这种情况(onload()))。现在我需要做的是,当用户点击表单上的特定按钮时,我需要暂停.exe文件的执行,当他们点击另一个按钮时,我们需要继续执行。我该怎么做?

感谢您的帮助。

C#process.start()暂停执行-BEGINNER

来自C#中的挂起进程

 [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);

    private void SuspendProcess(int PID)
    {
      Process proc = Process.GetProcessById(PID);
      if (proc.ProcessName == string.Empty)
        return;
      foreach (ProcessThread pT in proc.Threads)
      {
        IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
        if (pOpenThread == IntPtr.Zero)
        {
          break;
        }
        SuspendThread(pOpenThread);
      }
    }
    public void ResumeProcess(int PID)
    {
      Process proc = Process.GetProcessById(PID);
      if (proc.ProcessName == string.Empty)
        return;
      foreach (ProcessThread pT in proc.Threads)
      {
        IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
        if (pOpenThread == IntPtr.Zero)
        {
          break;
        }
        ResumeThread(pOpenThread);
      }
    }

看看这篇文章,它讲述了如何颠覆流程。

SO上的这个问题也和你想问的一样,看看吧。

在进程上尝试WaitForExit()。