2010-10-25 16 views
5

Estoy usando el siguiente fragmento de código para detener un servicio. Sin embargo, las dos declaraciones Console.Writeline indican que el servicio se está ejecutando. ¿Por qué no se detendrá el servicio?El servicio de Windows no se detiene/inicia

class Program 
{ 
    static void Main(string[] args) 
    { 
     string serviceName = "DummyService"; 
     string username = ".\\Service_Test2"; 
     string password = "Password1"; 

     ServiceController sc = new ServiceController(serviceName); 

     Console.WriteLine(sc.Status.ToString()); 

     if (sc.Status == ServiceControllerStatus.Running) 
     { 
      sc.Stop(); 
     } 

     Console.WriteLine(sc.Status.ToString()); 
    } 
} 
+0

¿Dónde se utiliza usuario y contraseña ?? – Aliostad

+0

Lo uso más abajo en el código donde cambio la cuenta y la contraseña asociadas con el servicio. – xbonez

Respuesta

5

Para actualizar el estado, debe llamar al sc.Refresh(). Consulte http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.stop.aspx para obtener más información.

Además, el servicio puede tardar un tiempo en detenerse. Si el método devuelve de inmediato, puede ser útil para cambiar su parada en algo como esto:

// Maximum of 30 seconds. 

for (int i = 0; i < 30; i++) 
{ 
    sc.Refresh(); 

    if (sc.Status.Equals(ServiceControllerStatus.Stopped)) 
     break; 

    System.Threading.Thread.Sleep(1000); 
} 
+0

funciona. ¡Gracias un montón! – xbonez

1

intentar llamar a:

sc.Refresh(); 

antes de su llamada al Estado.

2

Llame a sc.Refresh() antes de verificar el estado. También puede llevar un tiempo detenerse.

0

¿Tiene los derechos correctos para detener/iniciar un servicio?

¿En qué cuenta está ejecutando su aplicación de consola como? Derechos de administrador?

¿Has probado sc.WaitForStatus? Es posible que el servicio se detenga, pero no antes de que llegue a su línea de escritura.

1

creo que puedes usar sc.stop y actualice http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.refresh(VS.80).aspx

// If it is started (running, paused, etc), stop the service. 
// If it is stopped, start the service. 
ServiceController sc = new ServiceController("Telnet"); 
Console.WriteLine("The Telnet service status is currently set to {0}", 
        sc.Status.ToString()); 

if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || 
    (sc.Status.Equals(ServiceControllerStatus.StopPending))) 
{ 
    // Start the service if the current status is stopped. 

    Console.WriteLine("Starting the Telnet service..."); 
    sc.Start(); 
} 
else 
{ 
    // Stop the service if its status is not set to "Stopped". 

    Console.WriteLine("Stopping the Telnet service..."); 
    sc.Stop(); 
} 

// Refresh and display the current service status. 
sc.Refresh(); 
Console.WriteLine("The Telnet service status is now set to {0}.", 
        sc.Status.ToString()); 
1

intente lo siguiente:

while (sc.Status != ServiceControllerStatus.Stopped) 
{ 
    Thread.Sleep(1000); 
    sc.Refresh(); 
} 
Cuestiones relacionadas