protected override void OnClosing(CancelEventArgs e) { if (SimulationWorker.IsCompleted) { Simulation.Complete(); } else { e.Cancel = true; SimulationWorker.Stop().ContinueWith(task => this.InvokeAction(Close)); } }