예제 #1
0
        /// <summary>
        ///     Creates a listener for cooperative shutdown.
        /// </summary>
        /// <param name="shutdownRequested">
        ///     The callback that is invoked when cooperative shutdown has been
        ///     requested.
        /// </param>
        /// <returns>
        ///     A disposable representing the named pipe listener.
        /// </returns>
        public static async Task <IDisposable> Listen(Action shutdownRequested)
        {
            var listener = new CooperativeShutdownListener(
                GetPipeName(Process.GetCurrentProcess().Id),
                shutdownRequested);
            await listener.Listen();

            return(listener);
        }
        /// <summary>
        ///     Creates a listener for cooperative shutdown.
        /// </summary>
        /// <param name="shutdownRequested">
        ///     The callback that is invoked when cooperative shutdown has been
        ///     requested.
        /// </param>
        /// <param name="loggerFactory">
        ///     A logger factory.
        /// </param>
        /// <param name="onError">A method to be called if an error occurs while listening</param>
        /// <returns>
        ///     A disposable representing the named pipe listener.
        /// </returns>
        public static Task <IDisposable> Listen(Action shutdownRequested, ILoggerFactory loggerFactory, Action <Exception> onError = default)
        {
            var listener = new CooperativeShutdownListener(
                GetPipeName(Process.GetCurrentProcess().Id),
                shutdownRequested,
                loggerFactory.CreateLogger($"{nameof(LittleForker)}.{typeof(CooperativeShutdown).Name}"));

            Task.Run(async() =>
            {
                try
                {
                    await listener.Listen().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    onError?.Invoke(ex);
                }
            });

            return(Task.FromResult((IDisposable)listener));
        }