Windows Service restart through ServiceController class to achieve, see the specific http://msdn.microsoft.com/zh-cn/library/7fs05y7h (v = VS.80). aspx
The restart code is very simple. The C # code is similar to this one (ServiceController is a class in. NET Framework, and C # can also be used)
System. ServiceProcess. ServiceController class
Code
1 Sub Restart ()
2 Dim controller As New System. ServiceProcess. ServiceController ("ServiceName ")
3 LogText ("the service is stopping ...")
4 controller. Stop ()
5 LogText ("the Service has stopped ...")
6 controller. Start ()
7 LogText ("the Service has restarted ...")
8 End Sub
C # form: http://www.csharp-examples.net/restart-windows-service/
Start service
1 public static void StartService( string serviceName, int timeoutMilliseconds)
2 {
3 ServiceController service = new ServiceController (serviceName);
4 try
5 {
6 TimeSpan timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds);
7
8 service. Start ();
9 service. WaitForStatus ( ServiceControllerStatus . Running , timeout);
10 }
11 catch
12 {
13 // ...
14 }
15 }
Stop service
1 public static void StopService( string serviceName, int timeoutMilliseconds)
2 {
3 ServiceController service = new ServiceController (serviceName);
4 try
5 {
6 TimeSpan timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds);
7
8 service. Stop ();
9 service. WaitForStatus ( ServiceControllerStatus . Stopped , timeout);
10 }
11 catch
12 {
13 // ...
14 }
15 }
Restart service
1 public static void RestartService( string serviceName, int timeoutMilliseconds)
2 {
3 ServiceController service = new ServiceController (serviceName);
4 try
5 {
6 int millisec1 = Environment .TickCount;
7 TimeSpan timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds);
8
9 service. Stop ();
10 service.WaitForStatus( ServiceControllerStatus .Stopped, timeout);
11
12 // count the rest of the timeout
13 int millisec2 = Environment .TickCount;
14 timeout = TimeSpan .FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
15
16 service. Start ();
17 service.WaitForStatus( ServiceControllerStatus .Running, timeout);
18 }
19 catch
20 {
21 // ...
22 }
23 }