David Bentley
David Bentley

Reputation: 864

How to monitor another .exe and perform action if it closes

Currently have a program that opens another program. I need to perform an action should that program close. I am pretty new to C# so could use some help getting me pointed in the correct direction.

EDIT: So I am able to get the current form to open the external program. This form then needs to close and another form needs to have a function to perform an action should the loader.exe close. This is what I have in the first form. How do I code the other form to know if this program has ended.

public static class Program
{
    public static bool OpenManager { get; set; }
    public static int DeskNumber { get; set; }

    /// The main entry point for the application.

    [STAThread]
    static void Main(string[] arguments)
    {
        /// Do not move this!
        OpenManager = false;
        MYvariable = 0;
        /// ----------------

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Loader());

        if (OpenManager)
        {
            if (MYvariable == 1)
            {
                System.Diagnostics.Process startProgram = System.Diagnostics.Process.Start("loader.exe", "-r 52");
                startProgram.EnableRaisingEvents = true;
                Application.Run(new Manager());

Upvotes: 1

Views: 2831

Answers (3)

René Vogt
René Vogt

Reputation: 43886

Assuming you use the Process class to start the other program, you can subscribe to the Exited event of the Process instance:

Process myProcess = Process.Start("myprogram.exe", "arguments");
myProcess.EnableRaisingEvents = true;
myProcess.Exited += (sender, e) => 
    {
       // Do what you want to do when the process exited.
    }

Or, to do it more explicit, declare an explicit event handler that is called when the process finishes:

public void OnProcessExited(object sender, EventArgs e)
{
     // Do what you want to do when the process exited.
}

public void StartProcess()
{
    Process myProcess = Process.Start("myprogram.exe", "arguments");
    myProcess.EnableRaisingEvents = true;
    myProcess.Exited += OnProcessExited;
}

Upvotes: 2

user6438653
user6438653

Reputation:

If the application returns an int code you can use:

Process P = Process.Start("App.exe");
P.WaitForExit();
int code = P.ExitCode;

An application that has return:

public static int Main(string[] args)
{
    return (int);
}

Will set P.ExitCode to the return value when it closes.

Upvotes: 0

Sam
Sam

Reputation: 200

if you are using the Process class within c# to open and keep a track on the opened program you can handle its exited event like so:

App.Exited += AppOnExited;

private static void AppOnExited(object sender, EventArgs eventArgs)
{
    // Do your action here
}

App being the name of your process object.

dont forget you may need to remove the handler when your done like so:

App.Exited -= AppOnExited;

Upvotes: 0

Related Questions