Beispiel #1
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            if (IsInteractiveMode)
            {
                AppLifeTime?.ApplicationStarted.Register(() =>
                {
                    if (!IsHelpPrint)
                    {
                        Manager?.Startup(this, Configuration);
                        Manager?.Shutdown();
                        LogManager.Shutdown();
                    }
                    AppLifeTime?.StopApplication();
                });
                return(Task.CompletedTask);
            }

            AppLifeTime?.ApplicationStarted.Register(() => {
                Logger?.Info($"Service {ServiceName} started");
                try
                {
                    if (Manager != null)
                    {
                        if (!Manager.Startup(this, Configuration))
                        {
                            AppLifeTime.StopApplication();
                        }
                        Logger?.Info("Service initialization done");
                    }
                }
                #pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
                {
                    Logger?.Error($"Unmanaged exception happens during service start: \n{ex}");
                    AppLifeTime.StopApplication();
                }
                #pragma warning restore CA1031 // Do not catch general exception types
            });
            AppLifeTime?.ApplicationStopped.Register(() =>
            {
                Manager?.Shutdown();
                Logger?.Info($"Service {ServiceName} shuted down");
                LogManager.Shutdown();
            });
            return(Task.CompletedTask);
        }
        protected override async Task ExecuteAsync(CancellationToken token)
        {
            if (!RecorderSettings.RecordingEnabled)
            {
                return;
            }

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Console.WriteLine($"Started {nameof( MatchRecorderService.ExecuteAsync )}");

            Task.Factory.StartNew(async() =>
            {
                while (!token.IsCancellationRequested)
                {
                    CheckMessages();
                    RecorderHandler?.Update();
                    if (DuckGameProcess == null || DuckGameProcess.HasExited)
                    {
                        break;
                    }
                    await Task.Delay(TimeSpan.FromMilliseconds(100), token);
                }

                //wait 5 seconds for stuff to completely be done
                CancellationTokenSource fiveSecondsSource = new CancellationTokenSource();
                fiveSecondsSource.CancelAfter(TimeSpan.FromSeconds(5));

                StopRecordingRound();
                StopRecordingMatch();

                while (RecorderHandler.IsRecording && !fiveSecondsSource.Token.IsCancellationRequested)
                {
                    RecorderHandler?.Update();
                    await Task.Delay(TimeSpan.FromMilliseconds(100), fiveSecondsSource.Token);
                }

                //request the app host to close the process
                AppLifeTime.StopApplication();
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            await Task.CompletedTask;
#pragma warning restore CS4014
        }