Link to home
Create AccountLog in
Avatar of Dynotoe
Dynotoe

asked on

Close external application from c#

Hi everyone,

I am using a little c# windows app to launch an external application after clicking a button like so

private void button3_MouseClick(object sender, MouseEventArgs e)
        {
            Process charting = new Process();

            charting.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\AmiBroker\";
            charting.StartInfo.FileName = "Broker.exe";
            charting.StartInfo.Arguments = "ProcessStart.cs";

            charting.Start();

        }

Now I can't figure out how to close the external application (ideally) from the same button.  Can someone please help?

Let me thank you in advance for your gracious help.

Cheers - Dyno
Avatar of sreerajrs
sreerajrs

Hi Dyno,

try this
charting.WaitForExit(2000);
charting.Kill();
regards
sreeraj
Your code can be modified as
private void button3_MouseClick(object sender, MouseEventArgs e)
        {
            Process charting = new Process();

            charting.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\AmiBroker\";
            charting.StartInfo.FileName = "Broker.exe";
            charting.StartInfo.Arguments = "ProcessStart.cs";
            charting.Start();
           charting.WaitForExit(2000);//you can give the approximate time to complete the process as        
          // argument to waitforexit function
if(charting!=null)
{
            charting.Kill();
}

        }
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Dynotoe

ASKER

Perfect as always.  I wish you were in Boston.
Cheers - Sean