@@ -219,6 +219,9 @@ public MainWindow()
219219 // Add handler for tab selection changed to clear new content indicator
220220 tabControl . SelectionChanged += TabControl_SelectionChanged ;
221221
222+ // Add handler for window closing to clean up resources
223+ this . Closing += MainWindow_Closing ;
224+
222225 // Automatically load the active profile if one exists
223226 if ( ! string . IsNullOrEmpty ( AppConfig . ActiveProfile ) && File . Exists ( AppConfig . ActiveProfile ) )
224227 {
@@ -1835,5 +1838,52 @@ protected void OnPropertyChanged(string propertyName)
18351838 {
18361839 PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
18371840 }
1841+
1842+ private void MainWindow_Closing ( object ? sender , System . ComponentModel . CancelEventArgs e )
1843+ {
1844+ // Clean up file and folder watchers
1845+ if ( fileWatcher != null )
1846+ {
1847+ try
1848+ {
1849+ fileWatcher . Changed -= OnSingleFileChanged ;
1850+ fileWatcher . Dispose ( ) ;
1851+ fileWatcher = null ;
1852+ }
1853+ catch ( Exception ex )
1854+ {
1855+ Debug . WriteLine ( $ "Error disposing fileWatcher: { ex . Message } ") ;
1856+ }
1857+ }
1858+
1859+ if ( folderWatcher != null )
1860+ {
1861+ try
1862+ {
1863+ folderWatcher . Changed -= OnFolderFileChanged ;
1864+ folderWatcher . Created -= OnFolderFileCreated ;
1865+ folderWatcher . Error -= OnFolderWatcherError ;
1866+ folderWatcher . Dispose ( ) ;
1867+ folderWatcher = null ;
1868+ }
1869+ catch ( Exception ex )
1870+ {
1871+ Debug . WriteLine ( $ "Error disposing folderWatcher: { ex . Message } ") ;
1872+ }
1873+ }
1874+
1875+ // Stop forced refresh timer if it's running
1876+ StopForcedRefresh ( ) ;
1877+
1878+ // Save current application configuration
1879+ try
1880+ {
1881+ Config . SaveConfig ( AppConfig ) ;
1882+ }
1883+ catch ( Exception ex )
1884+ {
1885+ Debug . WriteLine ( $ "Error saving configuration: { ex . Message } ") ;
1886+ }
1887+ }
18381888 }
18391889}
0 commit comments