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
C#

Avatar of undefined
Last Comment
Dynotoe

8/22/2022 - Mon
sreerajrs

Hi Dyno,

try this
charting.WaitForExit(2000);
charting.Kill();
regards
sreeraj
sreerajrs

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
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dynotoe

ASKER
Perfect as always.  I wish you were in Boston.
Cheers - Sean
Your help has saved me hundreds of hours of internet surfing.
fblack61