예제 #1
0
        static void Main(string[] args)
        {
            var source         = new CancellationTokenSource();
            var stockCollector = new StockCollector();

            try {
                var service = new StockCollectionService();
                var token   = source.Token;
                service.StartAsync(token).GetAwaiter().GetResult();
            } catch (Exception ex) {
                source.Cancel();
                Console.WriteLine("[Error] " + DateTime.Now + " " + ex.Message + ", \n" + ex.StackTrace);
            } finally {
                source.Cancel();
            }
        }
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var stockCollector = new StockCollector();
            var stockClient    = new StockClient();
            var observable     = stockCollector.StocksStream(TimeSpan.FromSeconds(3), stoppingToken)
                                 .Where(stocks => stocks.Any())
                                 .DistinctUntilChanged()
                                 .Do(stocks => stockClient.AddRange(stocks))
                                 .Catch <IEnumerable <Stock>, Exception> (ex => {
                Console.WriteLine("[Error] " + DateTime.Now + " Catch: " + ex.Message + " : " + ex.StackTrace);
                return(Observable.Empty <IEnumerable <Stock> > ());
            });

            using (var stocks = observable.Subscribe()) {
                Console.WriteLine(DateTime.Now + " Press any key to unsubscribe");
                observable.Wait();
            }
            return(Task.CompletedTask);
        }