Thursday, August 6, 2009

Kill a Windows Service or Process Thread

Use one of the following code snippets:

using System.Diagnostics;

. . .

private static void KillProcessThread(string processName)
{
    Process[] processes;
    processes = Process.GetProcessesByName(processName);
    foreach(Process proc in processes)
    {
        proc.Kill();
    }
}

 

using System.ServiceProcess;

. . .

private static void KillWindowsService(string serviceName)
{
        ServiceController sc = new ServiceController();
        sc.ServiceName = serviceName;
        sc.Stop();
}

No comments:

Post a Comment