public void ConfigurationParserBuildsPreloadSchedules()
        {
            var configuration = ConfigurationManager.OpenExeConfiguration("MetricAgent.exe");

            var parser = new ConfigurationParser();
            var schedules = parser.Parse(configuration);

            Assert.IsInstanceOf<ParsedSchedules>(schedules);
            Assert.AreEqual(7, schedules.PreloadSchedules.Count());            
        }
        private void Run(object state)
        {
            // Other side of thread boundary here, so want to trap all exceptions to avoid
            // bringing the app down without a chance of acting

            try
            {
                var configuration = ConfigurationManager.OpenExeConfiguration("MetricAgent.exe");

                var parser = new ConfigurationParser();

                var schedules = parser.Parse(configuration);

                foreach (var preload in schedules.PreloadSchedules)
                {
                    preload.RunOnce();
                }

                var tasks = new List<Task>();

                foreach (var schedule in schedules.Schedules)
                {
                    var currentSchedule = schedule;

                    tasks.Add(Task.Factory.StartNew(() => currentSchedule.Start(_cancellationToken), _cancellationToken));
                }

                try
                {
                    Task.WaitAny(tasks.ToArray());
                }
                catch (AggregateException ae)
                {
                    ae.Handle(x =>
                        {
                            Log.Error(ae.Message);

                            return false;
                        });
                }
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}\n{1}", e.Message, e.StackTrace));

                Console.WriteLine("Failed while running MetricAgent, see log for details");

                CancelTasks();
            }
        }