private void CoursesRefreshTimer_Elapsed(object sender, ElapsedEventArgs e) { bool refreshData = false; Globals.Courses.ToList().ForEach(x => //Get every course and loop through, x.Value = course { foreach (var y in x.Value.Classes) //Loop through every class in the course { if (y.EndDate <= DateTime.Now.Date && y.Over == false) //If we passed the end date AND the class is not over yet { if (!Globals.Instructors.ToList().Exists(z => z.Value.TeachingCourses.Contains(y))) { //Incase of the class having no instructor, should not happen as we MUST add the class with instructor. //Classes.RemoveCourseClass(y); //throw new Exception("There is no instructor assigned to " + y.Course.Name + " class, this shouldn't happen."); continue; } var ins = Globals.Instructors.ToList().Single(z => z.Value.TeachingCourses.Contains(y)).Value; //Get the class instructor int childrenCount = Globals.Children.ToList().Where(z => z.Value.RegisteredCourses.Contains(y)).Count(); //Get Children Count taking this class. Instructors.AddInstructorSalary(ins.Id, y.CourseId, x.Value.PricePerChild * x.Value.Cost * childrenCount / 100); //Add Salary foreach (var child in Globals.Children.ToList().Where(z => z.Value.RegisteredCourses.Contains(y))) //Remove every child from the class { Children.RemoveChildClass(y.Id); } y.Over = true; //Set over to true Classes.EditCourseClass(y); //Edit the class in the db set over to true refreshData = true; } } }); if (refreshData) { Globals.RefreshReferenceInformation(); //Refresh db information } }