There is a method that works when a button is pressed. It uses the async await construction.

private async void OkButton(object sender,RoutedEventArgs e) { //какая-то логика. for (int i = 0; i < ListOfFiles.Length; i++) { //какая-то логика. Window window=new Window(); window.Show(); await sem.WaitAsync();//где sem это SemaphoreSlim для того ,чтобы цикл не продолжал открывать новое окно,пока не закроется старое. //какая-то логика. } } 

So when you close the MainWindow when the Window is open. Exception is thrown. The exception is the application. I tried to sign the method on the MainWindow.Closed event, which:

  private void window_Closed(object sender, EventArgs e) { window.Close(); App.Current.Shutdown(); } 

But it did not help, when debugging, it turned out that even after calling the App.Current.Shutdown (); method. The OkButton method continues to work and when creating a new window, as the application terminated, in the Window designer, when the InitializeCompponent () method is called, it is displayed This error. I also tried to initialize the following property in App.xaml, with the following value ShutdownMode = "OnExplicitShutdown", but this did not help. Of course I can create a boolean variable and if it is false, do not create it in the for loop of the window. But I would like to understand why this is happening, and how it can be adequately Yes. When using the method, without an async operator, everything is fine.

I bring Stack Trace from an exception. Where AskReplace.xaml is a window window that is created in a loop.

at System.Windows.Application. GetResourcePackage (Uri packageUri) \ Leva \ Work \ Informatika \ CSharp \ My OPENSOURCE projects \ MusicSorter \ MusicSorter \ AskReplace.xaml: line 1 at MusicSorter.AskReplace..ctor (String userFileToReplace, Button buttonToCancel) in D: \ Leva \ Work \ Informatika \ CSharp \ My OPENSOURCE projects \ MusicSorter \ MusicSorter \ AskReplace.xaml.cs: line 59 at MusicSorter.MainWindow.CopyFile (String from, String to) in D: \ Leva \ Work \ Informatika \ CSharp \ My OPENSOURCE projects \ MusicSorter \ MusicSorter \ MainWindow. xaml.cs: line 218 at MusicSorter.MainWindow.d__10.MoveNext () in D: \ Leva \ Work \ Informatika \ CSharp \ My OPENSOURCE projects \ MusicSorter \ MusicSorter \ MainWindow.xaml.cs: line 181

  • And why did OnExplicitShutdown not help? It was supposed to help. What happens in this case? - VladD 6:43 pm
  • And still, result stack trace from an exception. - VladD 6:43 pm
  • When using OnExplicitShutdown, the same Exception "The Application object is being shut down". - Faradey Inimicos
  • And when do you call Shutdown ? If after that you open the window, then of course it’s bad, the application is already “dead”. You should in theory call Shutdown only when you are sure that you do not need more windows. - VladD 7:58 pm
  • Shutdown I run in a method that is subscribed to the MainWindow.Closed event. That is, when MainWindow closes, the method in which I launch Shutdown is called. And, in theory, the OkButton method should also terminate and not end the loop iteration. But it finishes creating If I do not use async await, then everything is fine. When I call Shutdown, the OkButton method breaks and the windows are closed without the above exception. - Faradey Inimicos

0