예제 #1
0
        /// <summary>
        /// Runs a game application and returns a Task that only completes when the token is triggered or shutdown is triggered.
        /// </summary>
        /// <param name="host">The <see cref="IGameHost"/> to run.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task RunAsync(this IGameHost host, CancellationToken token = default(CancellationToken))
        {
            // Wait for token shutdown if it can be canceled
            if (token.CanBeCanceled)
            {
                await host.RunAsync(token, startupMessage : null);

                return;
            }

            // If token cannot be canceled, attach Ctrl+C and SIGTERM shutdown
            var done = new ManualResetEventSlim(false);

            using (var cts = new CancellationTokenSource())
            {
                var shutdownMessage = host.Services.GetRequiredService <GameHostOptions>().SuppressStatusMessages ? string.Empty : "Application is shutting down...";
                using (var lifetime = new GameHostLifetime(cts, done, shutdownMessage: shutdownMessage))
                    try
                    {
                        await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down.");

                        lifetime.SetExitedGracefully();
                    }
                    finally
                    {
                        done.Set();
                    }
            }
        }
예제 #2
0
        /// <summary>
        /// Returns a Task that completes when shutdown is triggered via the given token, Ctrl+C or SIGTERM.
        /// </summary>
        /// <param name="host">The running <see cref="IGameHost"/>.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task WaitForShutdownAsync(this IGameHost host, CancellationToken token = default(CancellationToken))
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token))
                using (var lifetime = new GameHostLifetime(cts, done, shutdownMessage: string.Empty))
                    try
                    {
                        await host.WaitForTokenShutdownAsync(cts.Token);

                        lifetime.SetExitedGracefully();
                    }
                    finally
                    {
                        done.Set();
                    }
        }