Killing a process and stopping a service are two different things. A service may spawn other processes that will remain protracted. In addition, you effectively pull out the plug in the process. This does not give time to stop the grace, write everything to disk, etc.
Instead, you should use the Win32_Service WMI object to find your service. This is the StartService and StopService , which will allow you to stop and start it as needed.
Remember that this WMI object refers to services, not processes, so you will have to configure your code to stop it by the service name, not the process name. Something like that:
ConnectionOptions options = new ConnectionOptions(); options.Username = userName; options.Password = password; ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverFullName), options); scope.Connect(); ObjectQuery query = new ObjectQuery(string.Format(@"SELECT * FROM Win32_Service WHERE Name='{0}'",serviceToStop)); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); ManagementObjectCollection queryCollection = searcher.Get(); foreach (ManagementObject m in queryCollection) { m.InvokeMethod("StopService", null); }
Then you can use InvokeMethod in the StartService.
source share