wasSharpNET – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
27 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.ServiceProcess; |
||
9 | |||
10 | namespace wasSharpNET.Platform.Windows.Services |
||
11 | { |
||
12 | public static class Utilities |
||
13 | { |
||
14 | /// <summary> |
||
15 | /// Start or stop a service. |
||
16 | /// </summary> |
||
17 | /// <param name="service">the name of service</param> |
||
18 | /// <param name="running">the requested service state</param> |
||
19 | /// <returns>true if the action could be performed</returns> |
||
20 | public static void SetServiceRunning(string service, bool running) |
||
21 | { |
||
22 | var s = new ServiceController(service); |
||
23 | |||
24 | // Do not make any change in case the service status matches the requested service status. |
||
25 | if (s.Status.Equals(ServiceControllerStatus.Running) && running) |
||
26 | return; |
||
27 | |||
28 | if (s.Status.Equals(ServiceControllerStatus.Stopped) && !running) |
||
29 | return; |
||
30 | |||
31 | switch (running) |
||
32 | { |
||
33 | case true: |
||
34 | s.Start(); |
||
35 | break; |
||
36 | case false: |
||
37 | s.Stop(); |
||
38 | break; |
||
39 | } |
||
40 | |||
41 | // Default Windows timespan of 30 seconds for service status result. |
||
42 | s.WaitForStatus(running ? ServiceControllerStatus.Running : ServiceControllerStatus.Stopped, |
||
43 | TimeSpan.FromSeconds(60)); |
||
44 | } |
||
45 | } |
||
46 | } |