Alexey Titov
Alexey Titov

Reputation: 97

WPF application cleanup on exit

I have a WPF MVVM app. It has a View and relevant ViewModel. ViewModel has an ObservableCollection of SerialPorts. Closing each port takes few seconds. Now I close my app just hitting the 'X' button - and if I use app.OnExit - my ViewModel is already disposed. When debugging - this sometimes gives me all sorts of compiler's strange behaviors, I give up trying to understand/fix them all.

On app's close/exit - how can I call some method in my ViewModel, to close all ports (which can take minutes), and do some other cleanup?

Upvotes: 2

Views: 1774

Answers (1)

Troels Larsen
Troels Larsen

Reputation: 4651

In any given Window, you can use the Window.Closing event.

Upvotes: 4

Related Questions