Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Stopping a service
Message
 
To
18/08/2010 14:39:25
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01477126
Message ID:
01477968
Views:
43
>What is the proper way of stopping a service? If I do Stop, this generates two errors in the Event Viewer. So, obviously, this is not the proper way of doing that. Anyone has done that from the code?

Here's some code I use:
public static void StopService(string serviceName, int timeoutSeconds)

{
    using (ServiceController controller = new ServiceController(serviceName))
    {
        try
        {
            controller.Stop();
            controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(timeoutSeconds));
        }
        catch (InvalidOperationException ex)
        {
            // Service may already be stopped
        }
    }            
}
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform