How to make GUI wait for windows service?

前端 未结 5 1308
轻奢々
轻奢々 2021-01-18 03:35

I wrote a windows service and a gui for it. Of course gui mainly depends on the service. Is there a way for gui to wait for the service? Sometimes I need to reload service c

相关标签:
5条回答
  • 2021-01-18 03:44

    Use a kernel Event object. When you start both apps, have them create or open a named Event object, then wait on it. The other can signal it, flipping the state thus allowing the other app to stop waiting and run.

    0 讨论(0)
  • 2021-01-18 03:45

    ServiceController has a method WaitForStatus where you pass it an argument of type ServiceControllerStatus. You can use it like this:

    controller.WaitForStatus(ServiceControllerStatus.Running);
    
    0 讨论(0)
  • 2021-01-18 03:46
    ServiceController mysqlServiceController = new ServiceController();
    mysqlServiceController.ServiceName = "MySql";
    var timeout = 3000;
    
    myServiceController.Start();
    
    try
    {
        //Wait till the service runs mysql      
        ServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, timeout, 0));
    }
    catch (System.ServiceProcess.TimeoutException)
    {
        MessageBox.Show(string.Format("Starting the service \"{0}\" has reached to a timeout of ({1}) minutes, please check the service.", mysqlServiceController.ServiceName, timeout));
    }
    
    0 讨论(0)
  • 2021-01-18 03:48

    Use an EventWaitHandle. Your GUI can wait on the WaitHandle and the service will set it which will trigger the GUI to continue on with what it was doing before it started waiting. No polling, no looping, no mess.

    This great article on C# threading is probably a better resource for info on WaitHandles

    0 讨论(0)
  • 2021-01-18 04:05

    I'd probably spawn a seperate thead to simply poll and see when your service controller status has changed, when the change occurs kill this thread. Then simply re spawn the thread when you need to start re-poll

    Darknight

    0 讨论(0)
提交回复
热议问题