public void Dispose() { Logger.Monitor.Info($"UIMonitor Dispose Begin"); if (_syncContextAuto == null) { return; } // Send is not supported on _syncContextAuto _syncContextAuto.Send(delegate { // Remove all event handlers ASAP Automation.RemoveAllEventHandlers(); if (_windowWatcher != null) { // _windowWatcher.MainWindowChanged -= _windowWatcher_MainWindowChanged; // _windowWatcher.SelectDataSourceWindowChanged -= _windowWatcher_SelectDataSourceWindowChanged; _windowWatcher.Dispose(); _windowWatcher = null; } if (_formulaEditWatcher != null) { _formulaEditWatcher.StateChanged -= _formulaEditWatcher_StateChanged; _formulaEditWatcher.Dispose(); _formulaEditWatcher = null; } if (_popupListWatcher != null) { _popupListWatcher.SelectedItemChanged -= _popupListWatcher_SelectedItemChanged; _popupListWatcher.Dispose(); _popupListWatcher = null; } }, null); // Let the above delegate and nested calls run, then clean up. // (not sure it makes a difference anymore...) _syncContextAuto.Post(delegate { _syncContextAuto.Complete(); _syncContextAuto = null; }, null); Logger.Monitor.Info($"UIMonitor Dispose End"); }
// Must run on the main thread public void Dispose() { Debug.Assert(Thread.CurrentThread.ManagedThreadId == 1); Logger.Monitor.Info($"UIMonitor Dispose Begin"); IntelliSenseEvents.Instance.OnIntellisenseInvalidated -= OnNewIntelliSenseDataAvailable; // Remove all event handlers ASAP // Since we are running on the main thread, we call Dispose directly // (might not be in a context where we can post to or wait for main thread sync context) if (_windowWatcher != null) { _windowWatcher.Dispose(); _windowWatcher = null; } if (_formulaEditWatcher != null) { _formulaEditWatcher.StateChanged -= _formulaEditWatcher_StateChanged; _formulaEditWatcher.Dispose(); _formulaEditWatcher = null; } if (_popupListWatcher != null) { _popupListWatcher.SelectedItemChanged -= _popupListWatcher_SelectedItemChanged; _popupListWatcher.Dispose(); _popupListWatcher = null; } if (_syncContextAuto == null) { Debug.Fail("Unexpected"); return; } // Let the above delegate and nested calls run, then clean up. // (not sure it makes a difference anymore...) _syncContextAuto.Post(delegate { _syncContextAuto.Complete(); _syncContextAuto = null; }, null); Logger.Monitor.Info($"UIMonitor Dispose End"); }