Benutzer mit den meisten Antworten
ServiceController/ServiceControllerStatus, timeout einstellen?

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
- Bearbeitet Robert BreitenhoferModerator Montag, 20. Juni 2011 12:55 Formatierung
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
-
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
-
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-timeoutexceptionGruß
Marcel- Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28
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
-
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
-
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-timeoutexceptionGruß
Marcel- Als Antwort markiert Purclot Montag, 20. Juni 2011 12:28