0

I have a C# forms application, and it has few buttons on it. I have used Process.start to execute a .exe program (this hapens as soon as the program is launched (onload())). Now what i need to do is, when a user clicks on a particular button on the form, i need to pause the execution of the .exe file and when they click on another button i need to resume execution. How should i do this ?

Help appreciated.

illep
  • 185
  • 2
  • 6
  • 13
  • 1
    What do you mean by pausing? Unless you have a mechanism inside the .exe you can't just halt a running process. – thekip Jun 23 '11 at 06:17
  • What do you mean by pausing the execution of another of another exe? I am afraid you can't do it until the exe you calling provide such functionality? – FIre Panda Jun 23 '11 at 06:17
  • for example to temporarily stop the execution of the process. Is that possible or is there any approach where i could achieve this – illep Jun 23 '11 at 06:20

3 Answers3

2

From Suspend Process in 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);
      }
    }
Community
  • 1
  • 1
user703016
  • 37,307
  • 8
  • 87
  • 112
0

Have a look on this article it tell how to susupend a process.

This question on SO also ask for the same as you want, have a look.

Community
  • 1
  • 1
Bibhu
  • 4,053
  • 4
  • 33
  • 63
-1

Try WaitForExit() on your Process.

Chaim Zonnenberg
  • 1,793
  • 13
  • 10
  • What WaitForExit will do accoring to u? – FIre Panda Jun 23 '11 at 06:22
  • I tried this, and it doesn't work `Process s = new Process(); // as a global variable . the following codes are in a button on click event s.StartInfo.FileName = "F:\\ss.exe "; s.StartInfo.UseShellExecute = false; s.StartInfo.CreateNoWindow = true; ` later in another button event i added `s.WaitForExit();` s.Start();`\ – illep Jun 23 '11 at 06:41
  • illep Process.WaitForExit(); will suspend you're application and block it where you placed (WaitForExit();) the Line until the Process is done ,so dont use that use these Native Codes on top of this Question . – Rosmarine Popcorn Jun 23 '11 at 07:35