How to make GUI wait for windows service?

前端 未结 5 1309
轻奢々
轻奢々 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: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));
    }
    

提交回复
热议问题