private void BeginSimulation(object sender, RoutedEventArgs e) { OptionsVis = Visibility.Collapsed; LoadingVis = Visibility.Visible; restartBtn.Visibility = Visibility.Collapsed; Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { Map = new Map((int)MapHeight, (int)MapWidth); MapGrid.DroneVision = DroneVision; MapGrid.Map = Map; IList <Drone> drones = new List <Drone>(); int startX = MapGrid.Map.HomeBase.XCenter; int startY = MapGrid.Map.HomeBase.YCenter; for (int i = 0; i < NumDrones; i++) { drones.Add(new Drone(startX, startY, DroneBattery, startX, startY)); } this.Controller = new ClassController(MapGrid, drones); Controller.DetermineFlight(); LoadingVis = Visibility.Collapsed; MapVis = Visibility.Visible; int step = DroneVision - 4; //Offset by size of the drone itself int numInstructions = (Controller.MaxInstructionsRemaining / step) + 1; for (int j = 0; j < numInstructions; j++) { Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { for (int _ = 0; _ < step; _++) { Controller.ControllerUpdate(); } MapGrid.UpdateMap(Controller.droneList); })); } MapGrid.UpdateMap(Controller.droneList); Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.ContextIdle, new Action(() => { restartBtn.Visibility = Visibility.Visible; })); })); }