Closing an External Program

Hello Everyone and thanks for your help in advance.  I have a third party program that is opened daily using Windows Scheduler, which in turn runs a process.  However, at the end of the process, the program does not close itself instead simply terminating and leaving itself open.  Because of this, Windows Scheduler fails on the next attempt to launch the program unless someone manually closes the program daily.  I know a Windows form can launch an external exe, and assume it can also be used to close an external program or process, however, I do not know how to do this.  Any insight on this topic would be greatly appreciated.

Answer this question

Closing an External Program

  • Bruce_Krasnof

    Thanks so much.  This is exactly what I needed.
  • MikeFrey

    Thanks for the help and the response.  I think you may have misunderstood part of the post.  The third party program is simply not designed to exit after it runs.  It is a very old type of program that launches a macro during its process, but simply does not close after running.  Will what you suggested still work

    Thanks again for the help.

  • Lajash

    As long as it is a .EXE that is running the example I gave will work. Replace the name Notepad with the name of your process. And yes, I understood correctly now, you cannot add the Environment.Exit code since its a 3rd party stuff.

    In case you want to also start the program then you would need to use Process.Start() method and pass the .EXE with path.

    Regards,
    Vikram

  • Asifkhan75025

    Hi,

    First of all, I feel you need to analyze why this particular program is terminating and not exiting correctly. If it is possible to make the application to gracefully exit, that would be a better approach. Try adding a Environment.Exit(0) explicitly in this program and see if it helps.

    However, if this is what u need to do then: Here is a code snippet in C# that does it. You need to import the namespace System.Diagnostics. The sample terminates an instance of notepad if it is running.


    Process[] arrProcesses = Process.GetProcessesByName("notepad");
    if (arrProcesses.Length > 0) 
                arrProcesses[0].Kill();


     



    Regards,
    Vikram



  • Dvorak Pavel

    Is there a graceful way to shut a program down besides CloseMainWindow   The problem I have is one of the apps I'm dealing with minimizes itself to the system tray.  I want to give it a chance to close gracefully, but CloseMainWindow does nothing since it doesn't really have a main window at the moment.

    If there was a way to activate the program, it would open its main window then I could call CloseMainWindow.  That would work most of the time, but some programs this app would be working with run strictly in the system try.  (ie, no main window at all.)

    Any suggestions


  • Closing an External Program