Пример #1
0
 private static Natives.ServiceState GetServiceStatus(IntPtr service)
 {
     Natives.SERVICE_STATUS status = new Natives.SERVICE_STATUS();
     if (Natives.QueryServiceStatus(service, status) == 0)
     {
         throw new Exception("QueryServiceStatus Failed.");
     }
     return(status.dwCurrentState);
 }
Пример #2
0
        private static void StopService(IntPtr service)
        {
            Natives.SERVICE_STATUS status = new Natives.SERVICE_STATUS();
            Natives.ControlService(service, Natives.ServiceControl.Stop, status);
            var changedStatus = WaitForServiceStatus(service, Natives.ServiceState.StopPending, Natives.ServiceState.Stopped);

            if (!changedStatus)
            {
                Console.WriteLine("Stop failed");
            }
        }
Пример #3
0
        private static void StartService(IntPtr service)
        {
            Natives.SERVICE_STATUS status = new Natives.SERVICE_STATUS();
            Natives.StartService(service, 0, 0);
            var changedStatus = WaitForServiceStatus(service, Natives.ServiceState.StartPending, Natives.ServiceState.Running);

            if (!changedStatus)
            {
                Console.WriteLine("Start failed");
            }
        }
Пример #4
0
        private static bool WaitForServiceStatus(IntPtr service, Natives.ServiceState waitStatus, Natives.ServiceState desiredStatus)
        {
            Natives.SERVICE_STATUS status = new Natives.SERVICE_STATUS();
            Natives.QueryServiceStatus(service, status);
            if (status.dwCurrentState == desiredStatus)
            {
                return(true);
            }
            int dwStartTickCount = Environment.TickCount;
            int dwOldCheckPoint  = status.dwCheckPoint;

            while (status.dwCurrentState == waitStatus)
            {
                int dwWaitTime = status.dwWaitHint / 10;
                if (dwWaitTime < 1000)
                {
                    dwWaitTime = 1000;
                }
                else if (dwWaitTime > 10000)
                {
                    dwWaitTime = 10000;
                }
                Thread.Sleep(dwWaitTime);
                if (Natives.QueryServiceStatus(service, status) == 0)
                {
                    break;
                }
                if (status.dwCheckPoint > dwOldCheckPoint)
                {
                    dwStartTickCount = Environment.TickCount;
                    dwOldCheckPoint  = status.dwCheckPoint;
                }
                else if (Environment.TickCount - dwStartTickCount > 10000)
                {
                    break;                                                       // status.dwWaitHint) break;
                }
            }
            return(status.dwCurrentState == desiredStatus);
        }