Come posso avviare e interrompere un servizio Windows da un'applicazione C# Form?Start stop Servizio dall'app modulo C#
risposta
Aggiungere un riferimento al System.ServiceProcess.dll
. Quindi è possibile utilizzare la classe ServiceController.
// Check whether the Alerter service is started.
ServiceController sc = new ServiceController();
sc.ServiceName = "Alerter";
Console.WriteLine("The Alerter service status is currently set to {0}",
sc.Status.ToString());
if (sc.Status == ServiceControllerStatus.Stopped)
{
// Start the service if the current status is stopped.
Console.WriteLine("Starting the Alerter service...");
try
{
// Start the service, and wait until its status is "Running".
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
// Display the current service status.
Console.WriteLine("The Alerter service status is now set to {0}.",
sc.Status.ToString());
}
catch (InvalidOperationException)
{
Console.WriteLine("Could not start the Alerter service.");
}
}
Si può fare in questo modo, Details of Service Controller
ServiceController sc = new ServiceController("your service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
Allo stesso modo si può smettere di usare il metodo di arresto
sc.Stop();
non riconosce questo using System.ServiceProcess; - sto usando .net 4 – AlexandruC
Aggiungi lo spazio dei nomi, probabilmente ti manca. – edocetirwi
Prima di tutto aggiungere un riferimento all'assembly System.ServiceProcess.
Per iniziare:
ServiceController service = new ServiceController("YourServiceName");
service.Start();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
Per fermare:
ServiceController service = new ServiceController("YourServiceName");
service.Stop();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Entrambi gli esempi mostrano come aspettare fino a quando il servizio ha raggiunto un nuovo status (in esecuzione, fermato ... ecc.). Il parametro di timeout in WaitForStatus è facoltativo.
non lo riconosce utilizzando System.ServiceProcess; - sto usando .net 4 – AlexandruC
Dovrebbe funzionare bene, ma devi aggiungere un riferimento a System.ServiceProcess. –
Giusto! sciocco me. Grazie. segnato! – AlexandruC
c'è un sporco, ma lo stesso stesso ..
basta eseguire il comando shell
NET STOP "MYSERVICENAME"
NET START "MYSERVICENAME"
// Check whether the U-TEST RTC service is started.
ServiceController sc = new ServiceController();
sc.ServiceName = "U-TEST RTC";
m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStatusService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
if (sc.Status == ServiceControllerStatus.Stopped)
{
m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
try
{
// Start the service, and wait until its status is "Running".
sc.Start();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgNowService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
}
catch (InvalidOperationException)
{
m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgExceptionStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
}
}
- 1. Start/Stop Servizio presso il Widget
- 2. Start service after force stop
- 3. Android: il servizio Stop/Start dipende dallo stato WiFi?
- 4. Node js start e stop windows services
- 5. ScheduledExecutorService start stop più volte
- 6. start-stop-daemon e pitone
- 7. Qual è la differenza tra servizio tomcat start/stop e ./catalina.sh run/stop
- 8. stop/start/pause in animazione python matplotlib
- 9. start-stop-daemon e programma java
- 10. "start-stop-daemon: impossibile fare stat"
- 11. Cosa fa 'adb shell start/stop' fare?
- 12. Start/Stop DispatcherTimer da un thread diverso
- 13. shell start/stop per script python
- 14. argomenti citati start-stop-daemon erroneamente interpretati
- 15. Successo con start-stop-daemon e mono-service2?
- 16. Come posso Stop/Start/Pausa una @JmsListener (il modo pulito)
- 17. Debian start-stop-daemon. Java iniziare vaso File
- 18. Media Player start start start
- 19. C# stop e continuare
- 20. Redis start as service [errore servizio non riconosciuto]
- 21. stop esecuzione di codice C#
- 22. jQuery Convalida il modulo di stop invia
- 23. Start-Service: impossibile avviare il servizio "Servizio host fabric di servizio Microsoft (FabricHostSvc)"
- 24. Eccezioni non stop in C#
- 25. Il servizio tomcat8 non è stato avviato utilizzando il servizio tomcat8 start
- 26. God stop resque workers rastrello
- 27. 'servizio postgresql start' non riesce ad avviare il servizio postgres su Fedora
- 28. Devo implementare il metodo Stop in un servizio Windows?
- 29. Stop/Avvio servizio nel codice in Windows 7
- 30. Android: Stop/avviare il servizio creato nel onCreate()
non riconosce questo utilizzando System.ServiceProcess; - sto usando .net 4 – AlexandruC
@lxClan aggiungi riferimento nel tuo progetto – Zbigniew
Ho aggiornato la risposta con il riferimento che devi aggiungere. –