예제 #1
0
        private void InternalStop()
        {
            _stopping = true;
            // stop in reverse start order
            if (Configuration.StopServicesAsync)
            {
                int maxWaitTime = Configuration.StopServicesMaxWaitTime;
                if (maxWaitTime <= 0)
                {
                    maxWaitTime = Timeout.Infinite;
                }
                Log("Service Host '" + OptionName + "' stopping asynchronously. Max wait time: " + ((maxWaitTime == Timeout.Infinite) ? "infinite" : maxWaitTime + " ms"));
                List <IAsyncResult> _results = new List <IAsyncResult>(_services.Count);
                List <WaitHandle>   _waits   = new List <WaitHandle>(_services.Count);
                foreach (IService service in _services)
                {
                    ServiceControllerStatus status = service.Status;
                    if (status == ServiceControllerStatus.Running || status == ServiceControllerStatus.Paused)
                    {
                        Log("Service '" + service.Name + "' stopping");
                        ServiceCallDelegate d      = new ServiceCallDelegate(ServiceStop);
                        IAsyncResult        result = d.BeginInvoke(service, null, service);
                        _results.Add(result);
                        _waits.Add(result.AsyncWaitHandle);
                    }
                }

                if (_waits.Count > 0)
                {
                    WaitHandle.WaitAll(_waits.ToArray(), maxWaitTime, Configuration.WaitExitContext);
                }

                foreach (IAsyncResult result in _results)
                {
                    IService service = (IService)result.AsyncState;
                    HandleServiceStopped(service);
                }
            }
            else
            {
                Log("Service Host '" + OptionName + "' stopping synchronously");
                foreach (IService service in _services)
                {
                    ServiceControllerStatus status = service.Status;
                    if (status == ServiceControllerStatus.Running || status == ServiceControllerStatus.Paused)
                    {
                        try
                        {
                            Log("Service '" + service.Name + "' stopping");
                            service.Stop();
                            Log("Service '" + service.Name + "' stopped");
                        }
                        catch (Exception e)
                        {
                            service.StopException = e;
                        }
                        HandleServiceStopped(service);
                    }
                }
            }

            Log("Service Host '" + OptionName + "' stopped.");
            EventLog.WriteEntry("Service Host '" + OptionName + "' was stopped successfully.", EventLogEntryType.Information);
            _closed.Set();
        }
예제 #2
0
        private void InternalStop()
        {
            _stopping = true;
            // stop in reverse start order
            if (Configuration.StopServicesAsync)
            {
                int maxWaitTime = Configuration.StopServicesMaxWaitTime;
                if (maxWaitTime <= 0)
                {
                    maxWaitTime = Timeout.Infinite;
                }
                Log("Service Host '" + OptionName + "' stopping asynchronously. Max wait time: " + ((maxWaitTime == Timeout.Infinite) ? "infinite" : maxWaitTime + " ms"));
                List<IAsyncResult> _results = new List<IAsyncResult>(_services.Count);
                List<WaitHandle> _waits = new List<WaitHandle>(_services.Count);
                foreach (IService service in _services)
                {
                    ServiceControllerStatus status = service.Status;
                    if (status == ServiceControllerStatus.Running || status == ServiceControllerStatus.Paused)
                    {
                        Log("Service '" + service.Name + "' stopping");
                        ServiceCallDelegate d = new ServiceCallDelegate(ServiceStop);
                        IAsyncResult result = d.BeginInvoke(service, null, service);
                        _results.Add(result);
                        _waits.Add(result.AsyncWaitHandle);
                    }
                }

                if (_waits.Count > 0)
                {
                    WaitHandle.WaitAll(_waits.ToArray(), maxWaitTime, Configuration.WaitExitContext);
                }

                foreach (IAsyncResult result in _results)
                {
                    IService service = (IService)result.AsyncState;
                    HandleServiceStopped(service);
                }
            }
            else
            {
                Log("Service Host '" + OptionName + "' stopping synchronously");
                foreach (IService service in _services)
                {
                    ServiceControllerStatus status = service.Status;
                    if (status == ServiceControllerStatus.Running || status == ServiceControllerStatus.Paused)
                    {
                        try
                        {
                            Log("Service '" + service.Name + "' stopping");
                            service.Stop();
                            Log("Service '" + service.Name + "' stopped");
                        }
                        catch (Exception e)
                        {
                            service.StopException = e;
                        }
                        HandleServiceStopped(service);
                    }
                }
            }

            Log("Service Host '" + OptionName + "' stopped.");
            EventLog.WriteEntry("Service Host '" + OptionName + "' was stopped successfully.", EventLogEntryType.Information);
            _closed.Set();
        }
예제 #3
0
        private void InternalStart(object args)
        {
            if (!string.IsNullOrEmpty(OptionLcid))
            {
                Extensions.SetCurrentThreadCulture(OptionLcid);
            }

            if (_services.Count == 0)
            {
                Log("Service Host '" + OptionName + "' has no hosted services configured. Stopping.");
                return;
            }

            if (Configuration.StartServicesAsync)
            {
                Log("Service Host '" + OptionName + "' starting asynchronously.");
                List <IAsyncResult> _results = new List <IAsyncResult>(_services.Count);
                List <WaitHandle>   _waits   = new List <WaitHandle>(_services.Count);
                foreach (IService service in _services)
                {
                    if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running)
                    {
                        Log("Service '" + service.Name + "' starting");
                        ServiceCallDelegate d      = new ServiceCallDelegate(ServiceStart);
                        IAsyncResult        result = d.BeginInvoke(service, null, service);
                        _results.Add(result);
                        _waits.Add(result.AsyncWaitHandle);
                    }
                }

                if (_waits.Count > 0)
                {
                    WaitHandle.WaitAll(_waits.ToArray(), Timeout.Infinite, Configuration.WaitExitContext);
                }

                int successCount = 0;
                foreach (IAsyncResult result in _results)
                {
                    IService service = (IService)result.AsyncState;
                    if (service.StartException == null)
                    {
                        successCount++;
                    }
                    HandleServiceStarted(service);
                }
                if (successCount == 0)
                {
                    Log("Service Host '" + OptionName + "' has no hosted services successfully started. Stopping.");
                    return;
                }
            }
            else
            {
                Log("Service Host '" + OptionName + "' starting");
                foreach (IService service in _services)
                {
                    try
                    {
                        if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running)
                        {
                            Log("Service '" + service.Name + "' starting");
                            service.Start();
                            Log("Service '" + service.Name + "' started");
                        }
                    }
                    catch (Exception e)
                    {
                        service.StartException = e;
                    }
                    HandleServiceStarted(service);
                }
            }

            Log("Service Host '" + OptionName + "' started");
            EventLog.WriteEntry("Service Host '" + OptionName + "' was started successfully.", EventLogEntryType.Information);
            _closed.Reset();

            if (!OptionService)
            {
                do
                {
                    ConsoleKeyInfo i;
                    try
                    {
                        i = Console.ReadKey(true);
                    }
                    catch (InvalidOperationException)
                    {
                        // ok, someone has killed the console, bail out
                        break;
                    }

                    if (i.Key == ConsoleKey.Enter || i.Key == ConsoleKey.Escape || i.KeyChar == 'q' || i.KeyChar == 'Q')
                    {
                        break;
                    }

                    if ((i.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
                    {
                        if (i.Key == ConsoleKey.Pause || i.KeyChar == 'c' || i.KeyChar == 'C')
                        {
                            break;
                        }
                    }

                    if (i.KeyChar == 'c' || i.KeyChar == 'C')
                    {
                        Console.Clear();
                    }
                }while (true);
            }
        }
예제 #4
0
        private void InternalStart(object args)
        {
            if (!string.IsNullOrEmpty(OptionLcid))
            {
                Extensions.SetCurrentThreadCulture(OptionLcid);
            }

            if (_services.Count == 0)
            {
                Log("Service Host '" + OptionName + "' has no hosted services configured. Stopping.");
                return;
            }

            if (Configuration.StartServicesAsync)
            {
                Log("Service Host '" + OptionName + "' starting asynchronously.");
                List<IAsyncResult> _results = new List<IAsyncResult>(_services.Count);
                List<WaitHandle> _waits = new List<WaitHandle>(_services.Count);
                foreach (IService service in _services)
                {
                    if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running)
                    {
                        Log("Service '" + service.Name + "' starting");
                        ServiceCallDelegate d = new ServiceCallDelegate(ServiceStart);
                        IAsyncResult result = d.BeginInvoke(service, null, service);
                        _results.Add(result);
                        _waits.Add(result.AsyncWaitHandle);
                    }
                }

                if (_waits.Count > 0)
                {
                    WaitHandle.WaitAll(_waits.ToArray(), Timeout.Infinite, Configuration.WaitExitContext);
                }

                int successCount = 0;
                foreach (IAsyncResult result in _results)
                {
                    IService service = (IService)result.AsyncState;
                    if (service.StartException == null)
                    {
                        successCount++;
                    }
                    HandleServiceStarted(service);
                }
                if (successCount == 0)
                {
                    Log("Service Host '" + OptionName + "' has no hosted services successfully started. Stopping.");
                    return;
                }
            }
            else
            {
                Log("Service Host '" + OptionName + "' starting");
                foreach (IService service in _services)
                {
                    try
                    {
                        if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running)
                        {
                            Log("Service '" + service.Name + "' starting");
                            service.Start();
                            Log("Service '" + service.Name + "' started");
                        }
                    }
                    catch (Exception e)
                    {
                        service.StartException = e;
                    }
                    HandleServiceStarted(service);
                }
            }

            Log("Service Host '" + OptionName + "' started");
            EventLog.WriteEntry("Service Host '" + OptionName + "' was started successfully.", EventLogEntryType.Information);
            _closed.Reset();

            if (!OptionService)
            {
                do
                {
                    ConsoleKeyInfo i;
                    try
                    {
                        i = Console.ReadKey(true);
                    }
                    catch (InvalidOperationException)
                    {
                        // ok, someone has killed the console, bail out
                        break;
                    }

                    if (i.Key == ConsoleKey.Enter || i.Key == ConsoleKey.Escape || i.KeyChar == 'q' || i.KeyChar == 'Q')
                        break;

                    if ((i.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
                    {
                        if (i.Key == ConsoleKey.Pause || i.KeyChar == 'c' || i.KeyChar == 'C')
                            break;
                    }

                    if (i.KeyChar == 'c' || i.KeyChar == 'C')
                    {
                        Console.Clear();
                    }
                }
                while (true);
            }
        }