Dante
Dante

Reputation: 3891

Close application opened in .NET console application

I'm opening an external application from a console application, as follows:

var p = new Process
{
    StartInfo =
    {
        FileName = "SomeApp.exe"
    }
};

p.Start();
Thread.Sleep(10000);
p.CloseMainWindow(); //Bam!! InvalidOperationExcetion

How can I close SomeApp.exe? I'm building some automation around this application and I would like it do be closed whenever I'm done with it, but I haven't been successful so far. p.CloseMainWindow() throws an exception of type InvalidOperationException with message "Cannot process request because the process (1216) has exited."

I'm not killing the process and I don't know what is killing it. I'm running this console application from Visual Studio in Administrator mode.

Any ideas? Thanks you in advance

Upvotes: 0

Views: 1178

Answers (3)

Original10
Original10

Reputation: 572

This will ask the application to close - this will allow it to ask the user if they want to save any open documents etc.

var p = new Process
{
    StartInfo =
    {
        FileName = "SomeApp.exe",
        ErrorDialog = true
    }
};

p.Start();
Thread.Sleep(5000);//or do whatever you need with the application
p.CloseMainWindow();
p.WaitForExit();

This will just end the process. You still have to WaitForExit() because Process.Kill() runs asynchronously

var p = new Process
{
    StartInfo =
    {
        FileName = "SomeApp.exe",
        ErrorDialog = true
    }
};

p.Start();
Thread.Sleep(5000);//or do whatever you need with the application
p.Kill();
p.WaitForExit();

update: added ErrorDialog = true to show a message if there is a problem starting the application

Upvotes: 1

Moha Dehghan
Moha Dehghan

Reputation: 18443

The call to WaitForExit() blocks the calling thread until the process exits. So there is nothing to kill afterwards!

You can check for the sate of the thread before calling Kill() using HasExited property, and call ClsoeMainWindow() to close the application. If that didn't work, Kill the process:

var p = new Process
{
    StartInfo =
    {
        FileName = "SomeApp.exe"
    }
};

p.Start();

// Do whatever...

if (!p.HasExited)
{
    if (!p.CloseMainWindow())
        p.Kill();
}

Upvotes: 1

BartoszKP
BartoszKP

Reputation: 35891

When you call .WaitForExit() your current process waits until the external application is closed. There is no point in calling .Kill() afterwards, because the process is already gone. A simple example:

        var p = new Process
        {
            StartInfo =
            {
                FileName = "cmd.exe"
            }
        };

        p.Start();  

        Console.WriteLine("press any key to kill the other app");
        Console.ReadKey();
        if (!p.HasExited)
        {
            p.Kill();
            Console.WriteLine("other app was killed");
        }
        else
        {
            Console.WriteLine("other app was already dead");
        }

        Console.ReadKey();

Depending on what you want, you may as well just skip the call to .Kill() in your current code - as I've mentioned, the external process is already gone after .WaitForExit().

Upvotes: 3

Related Questions