Exemplo n.º 1
0
        protected override void Run(CancellationToken token)
        {
            using (var watcher = new FileWatcher(File.FullName))
            {
                Configuration = GetConfiguration();

                var observable = Observable.FromEventPattern(
                    handler => watcher.Changed += handler,
                    handler => watcher.Changed -= handler
                    )
                                 .Throttle(TimeSpan.FromMilliseconds(200))
                                 .Subscribe(e =>
                {
                    Debug.WriteLine("Configuration file changed");
                    HandleUpdate();
                });

                watcher.Removed += (sender, e) =>
                {
                    Debug.WriteLine("Configuration file removed");
                    Service.Shutdown();
                };

                //wait until cancelled
                token.WaitHandle.WaitOne();
                Debug.WriteLine("Shutting down configuration watcher");
                observable.Dispose();
            }
        }
Exemplo n.º 2
0
        private void Wrap(CancellationToken token)
        {
            try
            {
                Debug.WriteLine("Attempting to run worker...");
                Run(token);
                Debug.WriteLine("Running worker finished normally...");
            }

            //catch operation cancellation
            catch (AggregateException exception)
                when(exception.InnerException is OperationCanceledException)
                {
                    Debug.WriteLine("Worker cancelled");
                }

            //catch operation cancellation
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Worker cancelled");
            }

            //catch aggregate exception
            catch (AggregateException exception)
            {
                Debug.WriteLine("Uncaught worker exceptions:");
                foreach (var innerException in exception.Flatten().InnerExceptions)
                {
                    Debug.WriteLine(innerException.Message);
                }
                Service.Shutdown();
            }

            //catch unhandled exceptions
            catch (Exception exception)
            {
                Debug.WriteLine("Uncaught worker exception: `{0}`", exception.Message);
                Service.Shutdown();
            }
            Debug.WriteLine("Running worker finished...");
            Stop();
        }