예제 #1
0
        /// <summary>
        /// Runs the given <see cref="AsyncServiceBase"/> service interactively
        /// </summary>
        /// <param name="service">The <see cref="AsyncServiceBase"/> service to run.</param>
        /// <param name="args">Commandline arguments passed to the service.</param>
        internal static void Run(this AsyncServiceBase service, string[] args)
        {
            if (!Environment.UserInteractive)
            {
                throw new NotSupportedException("Not running in UserInteractive mode.");
            }

            var thread = new AsyncContextThread();
            var task   = thread.Factory.Run(() => RunAsync(service, args));

            task.Wait();
            thread.Join();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var service = new SomeSampleService();

            if (Environment.UserInteractive)
            {
                AsyncServiceBase.RunInteractive(service);
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { service };
                ServiceBase.Run(ServicesToRun);
            }
        }
예제 #3
0
 /// <summary>
 /// Handle KeyPressed
 /// </summary>
 /// <param name="keyInfo"></param>
 /// <param name="service"></param>
 /// <param name="args"></param>
 private static void HandleKeyPressed(ConsoleKeyInfo keyInfo, AsyncServiceBase service, string[] args)
 {
     if (keyInfo.Key == ConsoleKey.P & service.Status == AsyncServiceStatus.Running)
     {
         service.Pause();
     }
     else if (keyInfo.Key == ConsoleKey.R & service.Status == AsyncServiceStatus.Stopped)
     {
         service.Start(args);
     }
     else if (keyInfo.Key == ConsoleKey.C & service.Status == AsyncServiceStatus.Paused)
     {
         service.Continue();
     }
     else if (keyInfo.Key == ConsoleKey.S & service.Status == AsyncServiceStatus.Running)
     {
         service.Stop();
     }
 }
예제 #4
0
        /// <summary>
        /// Execution task for the service
        /// </summary>
        /// <param name="service">The <see cref="AsyncServiceBase"/> service to run.</param>
        /// <param name="args">Commandline arguments passed to the service.</param>
        private static async Task RunAsync(AsyncServiceBase service, string[] args)
        {
            Console.WriteLine("Running interactive");

            service.Faulted       += Service_Faulted;
            service.StatusChanged += Service_StatusChanged;
            service.Start(args);

            while (true)
            {
                try
                {
                    var result = await ReadKeyAsync(CancellationToken.None);

                    HandleKeyPressed(result, service, args);
                }
                catch (OperationCanceledException)
                {
                    if (service.Status != AsyncServiceStatus.Stopped)
                    {
                        service.Stop();
                    }
                    break;
                }
                catch (TargetInvocationException e)
                {
                    Console.WriteLine(e.InnerException?.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            Console.WriteLine();
            Console.WriteLine($"Service ended with ExitCode {service.ExitCode}");
            Console.WriteLine("Press any key to quit");
            Console.ReadKey(true);
        }