public void CloseAplication() { quitButton.IsVisible = false; quititingButton.IsVisible = true; ViewModelContainer.DriveTestViewModel.BreakTest.Execute(null); AnimationBase.DisposeAllAnimations(); // dispose off all animation controls in order to avoid unhandled exception on app close in WPF (if any animation is running on close) if (ViewModelContainer.DriveTestViewModel.TestStarted) { Task.Run(() => { var wait = new SpinWait(); while (ViewModelContainer.DriveTestViewModel.TestStarted) { wait.SpinOnce(); } } ).ContinueWith((t) => { Thread.Sleep(1500); // if there's any animation in-progress, give it time to complete Device.BeginInvokeOnMainThread(() => { Application.Current.Quit(); });; } ); } else { Application.Current.Quit(); } }