Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit or end another executable

Tags:

c#

I have this code to run the exe:

String cPath = "C:\\GCOS\\HHT\\EXE\\" + frmSchemas.schema;
string cParams = HHTNUMBER+" "+ Login.user + "/" + Login.pass + "//" +Login.db + "//" + frmSchemas.schema ;
string filename = Path.Combine(cPath,"HHTCtrlp.exe"); 
Process.Start(filename, cParams); 

Now how do i end the program above?

like image 362
Privesh Avatar asked Sep 01 '26 23:09

Privesh


2 Answers

        Process[] processes = Process.GetProcessesByName("HHTCtrlp");
        foreach (var process in processes)
        {
            process.Kill();
        }
like image 84
RolandK Avatar answered Sep 03 '26 14:09

RolandK


Here's a sample from http://csharp-slackers.blogspot.com/2008/09/terminate-process.html

using System;
using System.Threading;
using System.Diagnostics;

public class TerminateProcessExample {

public static void Main () {

    // Create a new Process and run notepad.exe.
    using (Process process = Process.Start("notepad.exe")) {

        // Wait for 5 seconds and terminate the notepad process.
        Console.WriteLine("Waiting 5 seconds before terminating" +
            " notepad.exe.");
        Thread.Sleep(5000);

        // Terminate notepad process.
        Console.WriteLine("Terminating Notepad with CloseMainWindow.");

        // Try to send a close message to the main window.
        if (!process.CloseMainWindow()) {

            // Close message did not get sent - Kill Notepad.
            Console.WriteLine("CloseMainWindow returned false - " +
                " terminating Notepad with Kill.");
            process.Kill();

        } else {

            // Close message sent successfully; wait for 2 seconds
            // for termination confirmation before resorting to Kill.
            if (!process.WaitForExit(2000)) {

                Console.WriteLine("CloseMainWindow failed to" +
                    " terminate - terminating Notepad with Kill.");
                process.Kill();
            }
        }
    }

    // Wait to continue.
    Console.WriteLine("Main method complete. Press Enter.");
    Console.ReadLine();
}
}

As you can see, there are more graceful ways to try to terminate a process than just using Process.Kill();

like image 30
Armen Tsirunyan Avatar answered Sep 03 '26 16:09

Armen Tsirunyan