2013-05-01 15 views
15

Windows hizmetleri programlama hakkında: windows hizmetimi nasıl durdurulur? YaniProgram aracılığıyla Windows hizmet nasıl durdurulur

// Here is my service class (MyTestService.cs). 
public class MyTestService:ServiceBase{ 

    // Constructor. 
    public MyTestService(){ 
     this.ServiceName = "My Test Service"; 
     return; 
    } 
}; 

// My application class (ApplicationClass.cs). 
public static class ApplicationClass{ 

    // Here is main Main() method. 
    public static void Main(){ 
     // 1. Creating a service instance 
     // and running it using ServiceBase. 
     MyTestService service = new MyTestService(); 
     ServiceBase.Run(service); 
     // 2. Performing a test shutdown of a service. 
     service.Stop(); 
     Environment.Exit(0); 
     return; 
    }; 
}; 

:

İşte çok basitleştirilmiş bir örnek kod (C#) 'dir Ben sadece oluşturduk "Benim Testi Servisi" başlattığını ve durdu. Ama Services.msc'ye baktığımda - "Test Hizmetim" devam ediyor ve "Dur" bağlantısını tıkladığımda SADECE durur. Niye ya? - neden service.Stop() komutu hiçbir şey yapmıyor?

ServiceController.Stop() da hiçbir şey yapmaz!

Hizmetimi Main() yönteminden nasıl durdurabilirim?

+1

Uygulamanızı yönetici olarak çalıştırdığınızdan emin olun. – Icemanind

cevap

10

Stop işlevi bir sinyal gönderir. Sinyal alınana ve işlenene kadar beklemez.

Durma sinyali işini bitirene kadar beklemeniz gerekecek.

service.Stop(); 
service.WaitForStatus(ServiceControllerStatus.Stopped); 

fazla bilgi için bakınız:: Sen WaitForStatus arayarak bunu yapabilir http://msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx

Environment.Exit kötü biridir. BUNU KULLANMA! Uygulamanızı zor yoldan sonlandırır, son bloklarda herhangi bir temizleme yapmadan, GC tarafından sonlandırma yöntemlerini çağırmadan, diğer tüm yer üstü iplerini, vs. durdurur. Uygulamanızın durdurma sinyalinden önce uygulamanızın iptal edildiğini hayal edebiliyorum. .

6

Ben senin kod örneğinde projemde

public static ServiceController GetService(string serviceName) 
    { 
     ServiceController[] services = ServiceController.GetServices(); 
     return services.FirstOrDefault(_ => Contracts.Extensions.CompareStrings(_.ServiceName, serviceName)); 
    } 

    public static bool IsServiceRunning(string serviceName) 
    { 
     ServiceControllerStatus status; 
     uint counter = 0; 
     do 
     { 
      ServiceController service = GetService(serviceName); 
      if (service == null) 
      { 
       return false; 
      } 

      Thread.Sleep(100); 
      status = service.Status; 
     } while (!(status == ServiceControllerStatus.Stopped || 
        status == ServiceControllerStatus.Running) && 
       (++counter < 30)); 
     return status == ServiceControllerStatus.Running; 
    } 

    public static bool IsServiceInstalled(string serviceName) 
    { 
     return GetService(serviceName) != null; 
    } 

    public static void StartService(string serviceName) 
    { 
     ServiceController controller = GetService(serviceName); 
     if (controller == null) 
     { 
      return; 
     } 

     controller.Start(); 
     controller.WaitForStatus(ServiceControllerStatus.Running); 
    } 

    public static void StopService(string serviceName) 
    { 
     ServiceController controller = GetService(serviceName); 
     if (controller == null) 
     { 
      return; 
     } 

     controller.Stop(); 
     controller.WaitForStatus(ServiceControllerStatus.Stopped); 
    } 
2

içinde service.Stop() ve ServiceController.Stop() komutları aşağıdaki işlevleri kullanıyorum hizmet işlemi engelliyor ServiceBase.Run(service) beri çalışırken onlar aramadı çünkü hiçbir şey yapmaz ve sadece durağının üzerine döner hizmet.

İlgili konular