Exemple #1
0
        protected void OnStop(IWindowsServiceTask serviceTask)
        {
            LockAndTry(() =>
            {
                // Exit if another thread has already initiated shutdown.
                if (_runCancelSource.IsCancellationRequested)
                {
                    return(true);
                }

                _lazyConsoleLogger.Value.Debug("Stopping...");

                // Signal shutdown.
                _runCancelSource.Cancel();

                // Call OnStop for each service, same way windows would.
                using (var stopCancelSource = new CancellationTokenSource(StartStopTimeoutMin))
                {
                    var stopTasks = new Task[_taskPairs.Count];

                    for (var i = 0; i < _taskPairs.Count; i++)
                    {
                        stopTasks[i] = _taskPairs[i].ServiceTask.OnStopAsync(stopCancelSource.Token);
                    }

                    Task.WaitAll(stopTasks, StartStopTimeoutMax);
                }

                // Find...
                // 1) All OTHER service tasks (to avoid deadlock)
                // 2) that we should wait on stop
                // 3) and have tasks to wait on.
                // ...and then select their tasks into an array.
                var tasks = _taskPairs
                            .Where(p => !ReferenceEquals(p.ServiceTask, serviceTask))
                            .Where(p => p.ServiceTask.IsWaitOnStop)
                            .Where(p => p.Task != null)
                            .Select(p => p.Task)
                            .ToArray();

                // Wait on the other tasks.
                Task.WaitAll(tasks, StartStopTimeoutMax);

                _lazyConsoleLogger.Value.Debug("...Stopped");

                // Success if normal OnStop call from ServiceProcess, otherwise
                // this was unexpected and should cause a shutdown.
                return(serviceTask == null);
            });
        }
Exemple #2
0
        private async Task RunServiceAsync(IWindowsServiceTask serviceTask)
        {
            Exception exception = null;

            try
            {
                await serviceTask.RunAsync(_runCancelSource.Token).ConfigureAwait(false);
            }
            catch (TaskCanceledException ex)
            {
                if (!_runCancelSource.IsCancellationRequested)
                {
                    exception = ex;
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            if (exception != null)
            {
                if (Environment.UserInteractive)
                {
                    _lazyConsoleLogger.Value.Error(exception);
                }

                // ReSharper disable once InconsistentlySynchronizedField
                _registeredlogger.ErrorFormat("ConsoleWindowsService.RunServiceAsync - Unhandled Exception - Name: {0}", exception, serviceTask.Name);
            }

            // If specified, shutdown all serviecs.
            if (serviceTask.IsShutdownOnStop)
            {
                OnStop(serviceTask);
            }
        }
Exemple #3
0
 private ServiceTaskPair(IWindowsServiceTask serviceTask)
 {
     ServiceTask = serviceTask;
 }
Exemple #4
0
 public static ServiceTaskPair Create(IWindowsServiceTask serviceTask)
 {
     return(new ServiceTaskPair(serviceTask));
 }