Ejemplo n.º 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();
                    }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Runs a game application and block the calling thread until host shutdown.
 /// </summary>
 /// <param name="host">The <see cref="IGameHost"/> to run.</param>
 public static void Run(this IGameHost host) =>
 host.RunAsync().GetAwaiter().GetResult();