I have a WPF app and I want to execute some method in the Closing event. It can take several minutes, that's why the WPF window freezes. Here is my code:
public MainWindow() { InitializeComponent(); this.Closing += MainWindow_Closing; } void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Task.Run(new Action(ClaseAfterThreeSecond)).Wait(); } void ClaseAfterThreeSecond() { Thread.Sleep(3000); } I want to do not freeze the WPF app until MainWindow_Closing ends when I click the X button (close button) on the WPF app. Any suggestions?
EDIT: This way closes the window immediately. I don't need this:
async void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { await Task.Run(new Action(ClaseAfterThreeSecond)); }
await Task.Run(new Action(ClaseAfterThreeSecond)).CondigureAwait(false);. Also get rid ofThread.SleepinClaseAfterThreeSecondand change it toTask.Delay.