Answered Wpf application fails to close when to close

  • Thursday, December 27, 2012 2:06 PM
     
      Has Code

    I have a WPF application that uses the FileSystemWatcher. The watcher is created in a viewmodel, the viewmodel is set to the DataContext of my mainwindow.

    I have the following method to start watching in the ViewModel :

            public void StartWatching()
            {
                _watcher = new FileSystemWatcher(Folder) {Filter = "", NotifyFilter = NotifyFilters.LastAccess | 
                             NotifyFilters.LastWrite | 
                             NotifyFilters.FileName | 
                             NotifyFilters.DirectoryName};
                
                _watcherSubscription = Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(
                    h => _watcher.Created += h,
                    h => _watcher.Created -= h)
                    .ObserveOnDispatcher()
                    .Select(e=>e.EventArgs)
                    .Where(e=>e.ChangeType==WatcherChangeTypes.Created)
                    .Subscribe(FolderChanged2);
    
                _watcher.EnableRaisingEvents = true;    
                
            }

    When I close the application I call a method on my viewmodel : StopWatching

           public void StopWatching()
            {
    _watcherSubscription.Dispose(); _watcher.EnableRaisingEvents = false; _watcher.Dispose(); }

    But when I close the application, it seems to hang. When  I remove the part that creates the subscription, the application terminates normally. What am I doing wrong ?

All Replies

  • Thursday, December 27, 2012 3:05 PM
     
     
    Is StopWatching successfully running when called or is it hanging in the Dispose of the _watcher?
  • Thursday, December 27, 2012 3:09 PM
     
     Answered
    Sorry, the problem is solved, the cause was not the subscription, but a notify window that is shown when the event is triggered was not closed on closing the application.
    • Marked As Answer by rekna Thursday, December 27, 2012 3:09 PM
    •