none
Process.Start RRS feed

  • Question

  •  

    Hello,

     

    I see the Process.Start command; any way of figuring out when the process ends?  Is there callback I can't find?  I have to kick of a series of processes in order, and I need one to end, then the next one begins, etc.

     

    Thanks.

    Tuesday, February 19, 2008 8:29 PM

Answers

  • Here are two methods that I wrote in C#.NET to figure out when a process ends. You will need to know the process name or at least part of the process name, depending on which method that you use. Hope that this helps.

     

    /// <summary>

    /// Waits for the process to terminate or til the timeout is reached

    /// </summary>

    /// <param name="sProcessName">The name of the process (e.g. "Notepad")</param>

    /// <param name="timeout">Timeout in Seconds</param>

    /// <returns>True if the process was terminated;

    /// False if the process was not terminated in the allotted time.</returns>

    public bool WaitForProcessToTerminate(string sProcessName, int timeout)

    {

    Process[] processList;

    DateTime dStartTime = DateTime.Now;

    DateTime dCurrentTime = DateTime.Now;

    TimeSpan span = dCurrentTime.Subtract(dStartTime);

    while (span.Seconds < timeout)

    {

    processList = Process.GetProcessesByName(sProcessName);

    if (processList.Length == 0)

    {

    // Process is terminated

    return true;

    }

    Thread.Sleep(950);

    System.Windows.Forms.Application.DoEvents();

    dCurrentTime = DateTime.Now;

    span = dCurrentTime.Subtract(dStartTime);

    System.Diagnostics.Debug.Print("Seconds waiting = " + span.Seconds);

    }

    return false; // Process is not terminated

    } // WaitForProcessToTerminate(...)

     

    /// <summary>

    /// Waits for the process to terminate or til the timeout is reached

    /// </summary>

    /// <param name="sProcessName">The name of the process (e.g. "Notepad")</param>

    /// <param name="sCaption">Caption/Window Title</param>

    /// <param name="timeout">Timeout in Seconds</param>

    /// <returns>True if the process was terminated;

    /// False if the process was not terminated in the allotted time.</returns>

    public bool WaitForProcessToTerminate(string sProcessName, string sCaption, int timeout, MatchOptions match)

    {

    Process[] processList;

    DateTime dStartTime = DateTime.Now;

    DateTime dCurrentTime = DateTime.Now;

    TimeSpan span = dCurrentTime.Subtract(dStartTime);

    while (span.Seconds < timeout)

    {

    processList = Process.GetProcessesByName(sProcessName);

    if (processList.Length == 0)

    {

    return true; // Process is terminated

    }

    else // Check to see if the running processes have the associated 'caption' title

    {

    for (int ii = 0; ii < processList.Length; ii++)

    {

    if (processList[ii].ProcessName.ToUpper() == sProcessName.ToUpper())

    {

    switch (match)

    {

    case MatchOptions.Exact:

    if (processList[ii].MainWindowTitle.ToString() == sCaption)

    {

    goto PROCESS_STILL_RUNNING;

    //break; // The process is still running

    }

    break;

    case MatchOptions.Partial:

    if (processList[ii].MainWindowTitle.ToString().Contains(sCaption))

    {

    goto PROCESS_STILL_RUNNING;

    }

    break;

    default: // This should not happen

    break;

    } // switch (match)

    } // if processName =

    } // for (int ii

    return true; // The process is terminated

    } // else Check caption title

    PROCESS_STILL_RUNNING:

    Thread.Sleep(950);

    System.Windows.Forms.Application.DoEvents();

    dCurrentTime = DateTime.Now;

    span = dCurrentTime.Subtract(dStartTime);

    System.Diagnostics.Debug.Print("Seconds waiting = " + span.Seconds);

    }

    return false; // Process is not terminated

    } // WaitForProcessToTerminate(...)

    Tuesday, February 19, 2008 9:37 PM
  • This is actually quite easy. Did you have a look at the docs?

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.process_members.aspx

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;

    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process p = Process.Start("cmd.exe", "/C pause");
                p.Exited += new EventHandler(p_Exited);
                Console.WriteLine("Kill Process");
                p.Kill();
                p.WaitForExit();
                Console.WriteLine("Process has terminated as expected: {0}", p.HasExited);

            }

            static void p_Exited(object sender, EventArgs e)
            {
                Console.WriteLine("Process exited");
            }
        }
    }



    That should do the trick. You can use the exited event, or check the HasExited property.

    Yours,
       Alois Kraus




    Tuesday, February 19, 2008 10:28 PM

All replies

  • Here are two methods that I wrote in C#.NET to figure out when a process ends. You will need to know the process name or at least part of the process name, depending on which method that you use. Hope that this helps.

     

    /// <summary>

    /// Waits for the process to terminate or til the timeout is reached

    /// </summary>

    /// <param name="sProcessName">The name of the process (e.g. "Notepad")</param>

    /// <param name="timeout">Timeout in Seconds</param>

    /// <returns>True if the process was terminated;

    /// False if the process was not terminated in the allotted time.</returns>

    public bool WaitForProcessToTerminate(string sProcessName, int timeout)

    {

    Process[] processList;

    DateTime dStartTime = DateTime.Now;

    DateTime dCurrentTime = DateTime.Now;

    TimeSpan span = dCurrentTime.Subtract(dStartTime);

    while (span.Seconds < timeout)

    {

    processList = Process.GetProcessesByName(sProcessName);

    if (processList.Length == 0)

    {

    // Process is terminated

    return true;

    }

    Thread.Sleep(950);

    System.Windows.Forms.Application.DoEvents();

    dCurrentTime = DateTime.Now;

    span = dCurrentTime.Subtract(dStartTime);

    System.Diagnostics.Debug.Print("Seconds waiting = " + span.Seconds);

    }

    return false; // Process is not terminated

    } // WaitForProcessToTerminate(...)

     

    /// <summary>

    /// Waits for the process to terminate or til the timeout is reached

    /// </summary>

    /// <param name="sProcessName">The name of the process (e.g. "Notepad")</param>

    /// <param name="sCaption">Caption/Window Title</param>

    /// <param name="timeout">Timeout in Seconds</param>

    /// <returns>True if the process was terminated;

    /// False if the process was not terminated in the allotted time.</returns>

    public bool WaitForProcessToTerminate(string sProcessName, string sCaption, int timeout, MatchOptions match)

    {

    Process[] processList;

    DateTime dStartTime = DateTime.Now;

    DateTime dCurrentTime = DateTime.Now;

    TimeSpan span = dCurrentTime.Subtract(dStartTime);

    while (span.Seconds < timeout)

    {

    processList = Process.GetProcessesByName(sProcessName);

    if (processList.Length == 0)

    {

    return true; // Process is terminated

    }

    else // Check to see if the running processes have the associated 'caption' title

    {

    for (int ii = 0; ii < processList.Length; ii++)

    {

    if (processList[ii].ProcessName.ToUpper() == sProcessName.ToUpper())

    {

    switch (match)

    {

    case MatchOptions.Exact:

    if (processList[ii].MainWindowTitle.ToString() == sCaption)

    {

    goto PROCESS_STILL_RUNNING;

    //break; // The process is still running

    }

    break;

    case MatchOptions.Partial:

    if (processList[ii].MainWindowTitle.ToString().Contains(sCaption))

    {

    goto PROCESS_STILL_RUNNING;

    }

    break;

    default: // This should not happen

    break;

    } // switch (match)

    } // if processName =

    } // for (int ii

    return true; // The process is terminated

    } // else Check caption title

    PROCESS_STILL_RUNNING:

    Thread.Sleep(950);

    System.Windows.Forms.Application.DoEvents();

    dCurrentTime = DateTime.Now;

    span = dCurrentTime.Subtract(dStartTime);

    System.Diagnostics.Debug.Print("Seconds waiting = " + span.Seconds);

    }

    return false; // Process is not terminated

    } // WaitForProcessToTerminate(...)

    Tuesday, February 19, 2008 9:37 PM
  • Have you looked at Process.Exited event?
    Tuesday, February 19, 2008 10:26 PM
  • This is actually quite easy. Did you have a look at the docs?

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.process_members.aspx

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;

    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process p = Process.Start("cmd.exe", "/C pause");
                p.Exited += new EventHandler(p_Exited);
                Console.WriteLine("Kill Process");
                p.Kill();
                p.WaitForExit();
                Console.WriteLine("Process has terminated as expected: {0}", p.HasExited);

            }

            static void p_Exited(object sender, EventArgs e)
            {
                Console.WriteLine("Process exited");
            }
        }
    }



    That should do the trick. You can use the exited event, or check the HasExited property.

    Yours,
       Alois Kraus




    Tuesday, February 19, 2008 10:28 PM
  • That will work in this case.

     

    However, in my application, it wouldn't because I didn't actually start the process. So the code that I wrote would be able to detect any process that terminated, no matter who started it.

     

    Just an FYI. Hope that this helps.

     

    Tuesday, February 19, 2008 10:36 PM
  • Hello,

     

    Thank you all for that information.  That helps.  I was using the static overload, which doesn't let you tap into the event.

     

    Have a good one.

     

    Wednesday, February 20, 2008 2:04 PM
  • --I was using the static overload, which doesn't let you tap into the event

    Could you please clarify what exactly you mean by this?
    Thursday, February 21, 2008 3:37 AM
  • Hey,

     

    I was using the Process.Start static method (I said overload; meant to say method).  By the way, I tried assigning an anonymous method as the delegate to the Exited event, and it didn't fire.  Why would that be?  Does error conditions raise another event, or is Exited fired every time?

     

    Thanks.

     

    Thursday, February 21, 2008 2:08 PM
  • To troubleshoot this issue, we really need the source code and the detailed repro steps to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.


    Friday, February 22, 2008 3:20 AM
  • OK, I did some more debugging, and it is working.  So I got that down; however, there is a deeper issue that needs resolved, which I posted elsewhere.  Thanks.

    Friday, February 22, 2008 3:45 PM