none
ServiceController/ServiceControllerStatus, timeout einstellen? RRS feed

  • Frage

  • Hallo Forum,

    ich versuche mit dem ServiceController die SQL-Server Dienste starten und stoppen.
    Es funktioniert gut, wenn aber der Dienst (aus welchem Grund auch immer) sich nicht starten lässt, bleibt die Anwendung hängen. Kann man hier die Zeit für die Versuche nicht festlegen (wie z. B. bei ADO .NET timeout bei der Connection) nach der der Versuch elegant abgebrochen wird? Irgendwie finde ich nichts passendes...
    Der Code:

    public bool ServicesStoppen(int ServiceStopp, string instanzName)
     {
      bool result = true;
    
      ServiceController sc;
    
      if (instanzName == Default_Instanz)//Default-Instanz, die Services heissen anders als bei Named-Instanz...siehe oben bei Variablen-Def..
    
      {
      sc = new ServiceController(Default_Instanz);
      }
      else
      {
      sc = new ServiceController("SQLAGENT$" + instanzName);
      }
    
      try
      {
      if (sc.Status == ServiceControllerStatus.Running)
      {
       sc.Stop();
       sc.WaitForStatus(ServiceControllerStatus.Stopped);
      }
    
      if (instanzName == Default_Instanz)
      {
       sc.ServiceName = Default_Instanz;
      }
      else
      {
       sc.ServiceName = "MSSQL$" + instanzName;
      }
    
      if (sc.Status == ServiceControllerStatus.Running)
      {
       sc.Stop();
       sc.WaitForStatus(ServiceControllerStatus.Stopped);
      }
    //*****




    Vielen Dank für Eure Hilfe


    Montag, 20. Juni 2011 11:18

Antworten

  • Hallo,

    ServiceControlle.WaitForStatus(status) wartet solange, bis der gewünschte Status erreicht ist. Du suchst die Überladung ServiceController.WaitForStatus(status, timeout), bei der Du festlegen kannst, wie lange gewartet werden soll, bis die Methode zurückkehrt bzw. eine TimeOutException geworfen wird.


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    vb-faq.de
    • Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
    Montag, 20. Juni 2011 11:46
    Beantworter
  • Hallo P,

    Du kannst doch die Überscheibung mit dem TimeSpan-Parameter nehmen ...:

    [ServiceController.WaitForStatus-Methode (ServiceControllerStatus, TimeSpan) (System.ServiceProcess)]
    http://msdn.microsoft.com/de-de/library/35st9aw1.aspx

    ___________________

    intern ist WaitForStatus(ServiceControllerStatus, TimeSpan) übrigens folgendermassen implementiert:

    public void WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan timeout)
    {
      if (!Enum.IsDefined(typeof(ServiceControllerStatus), desiredStatus))
      {
        throw new InvalidEnumArgumentException("desiredStatus", (int) desiredStatus, typeof(ServiceControllerStatus));
      }
      DateTime utcNow = DateTime.UtcNow;
      this.Refresh();
      while (this.Status != desiredStatus)
      {
        if ((DateTime.UtcNow - utcNow) > timeout)
        {
          throw new TimeoutException(Res.GetString("Timeout"));
        }
        Thread.Sleep(250);
        this.Refresh();
      }
    }
    


    ciao Frank
    • Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
    Montag, 20. Juni 2011 11:46
  • Hallo P.,

    Sieh Dir auch die Erweiterungsmethode TryWaitForStatus() an, die ein bekanntes Problem bei der Verwendung von ServiceController.WaitForStatus umschifft.

    Justin Van Patten - Why does ServiceController.WaitForStatus throws a TimeoutException?
    http://connect.microsoft.com/VisualStudio/feedback/details/436389/why-does-servicecontroller-waitforstatus-throws-a-timeoutexception

    Gruß
    Marcel

    • Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
    Montag, 20. Juni 2011 12:16
    Moderator

Alle Antworten

  • Hallo,

    ServiceControlle.WaitForStatus(status) wartet solange, bis der gewünschte Status erreicht ist. Du suchst die Überladung ServiceController.WaitForStatus(status, timeout), bei der Du festlegen kannst, wie lange gewartet werden soll, bis die Methode zurückkehrt bzw. eine TimeOutException geworfen wird.


    Thorsten Dörfler
    Microsoft MVP Visual Basic
    vb-faq.de
    • Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
    Montag, 20. Juni 2011 11:46
    Beantworter
  • Hallo P,

    Du kannst doch die Überscheibung mit dem TimeSpan-Parameter nehmen ...:

    [ServiceController.WaitForStatus-Methode (ServiceControllerStatus, TimeSpan) (System.ServiceProcess)]
    http://msdn.microsoft.com/de-de/library/35st9aw1.aspx

    ___________________

    intern ist WaitForStatus(ServiceControllerStatus, TimeSpan) übrigens folgendermassen implementiert:

    public void WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan timeout)
    {
      if (!Enum.IsDefined(typeof(ServiceControllerStatus), desiredStatus))
      {
        throw new InvalidEnumArgumentException("desiredStatus", (int) desiredStatus, typeof(ServiceControllerStatus));
      }
      DateTime utcNow = DateTime.UtcNow;
      this.Refresh();
      while (this.Status != desiredStatus)
      {
        if ((DateTime.UtcNow - utcNow) > timeout)
        {
          throw new TimeoutException(Res.GetString("Timeout"));
        }
        Thread.Sleep(250);
        this.Refresh();
      }
    }
    


    ciao Frank
    • Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
    Montag, 20. Juni 2011 11:46
  • Hallo P.,

    Sieh Dir auch die Erweiterungsmethode TryWaitForStatus() an, die ein bekanntes Problem bei der Verwendung von ServiceController.WaitForStatus umschifft.

    Justin Van Patten - Why does ServiceController.WaitForStatus throws a TimeoutException?
    http://connect.microsoft.com/VisualStudio/feedback/details/436389/why-does-servicecontroller-waitforstatus-throws-a-timeoutexception

    Gruß
    Marcel

    • Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
    Montag, 20. Juni 2011 12:16
    Moderator
  • Hallo,
    vielen Dank für Eure Antworten. Hm, peinlich...ich habe die Überladung übersehen.. :-(

    Gruß P.

    Montag, 20. Juni 2011 12:29