using System.ComponentModel; using System.Windows; namespace SopHelper.Training; public partial class ModelTestWindow : Window { public ModelTestViewModel Model { get; } private bool _closing, _closedSafely; public ModelTestWindow(ModelTestViewModel model) { InitializeComponent(); Model = model; DataContext = model; } protected override async void OnClosing(CancelEventArgs e) { if (_closedSafely) { base.OnClosing(e); return; } e.Cancel = true; base.OnClosing(e); if (_closing) return; _closing = true; IsEnabled = false; await Model.StopAsync(); _ = Dispatcher.BeginInvoke(() => { _closedSafely = true; Close(); }); } }