@@ -246,13 +246,13 @@ void InstallTextChangeMonitor(AutomationElement element)
246246
247247 if ( isTextPatternAvailable )
248248 {
249- Logger . WindowWatcher . Info ( "FormulaEdit TextPattern adding change handler" ) ;
249+ Logger . WindowWatcher . Info ( "FormulaEdit/InCellEdit TextPattern adding change handler" ) ;
250250 Automation . AddAutomationEventHandler ( TextPattern . TextChangedEvent , element , TreeScope . Element , TextChanged ) ;
251251 _enableFormulaPolling = false ;
252252 }
253253 else
254254 {
255- Logger . WindowWatcher . Info ( "FormulaEdit TextPattern not available - enabling polling" ) ;
255+ Logger . WindowWatcher . Info ( "FormulaEdit/InCellEdit TextPattern not available - enabling polling" ) ;
256256 _enableFormulaPolling = true ; // Has an effect when focus changes
257257 UpdateFormulaPolling ( ) ;
258258 }
@@ -263,9 +263,15 @@ void UninstallTextChangeMonitor(AutomationElement element)
263263 bool isTextPatternAvailable = ( bool ) element . GetCurrentPropertyValue ( AutomationElement . IsTextPatternAvailableProperty ) ;
264264 if ( isTextPatternAvailable )
265265 {
266- Logger . WindowWatcher . Info ( "FormulaEdit TextPattern removing change handler" ) ;
266+ Logger . WindowWatcher . Info ( "FormulaEdit/InCellEdit removing text change handler" ) ;
267267 Automation . RemoveAutomationEventHandler ( TextPattern . TextChangedEvent , element , TextChanged ) ;
268268 }
269+ else
270+ {
271+ Logger . WindowWatcher . Info ( "FormulaEdit/InCellEdit disabling polling" ) ;
272+ _enableFormulaPolling = false ;
273+ UpdateFormulaPolling ( ) ;
274+ }
269275 }
270276
271277 // Breaks the PopupList SelectedItemChange event handler ... !?
@@ -300,13 +306,12 @@ void UpdateFormulaPolling()
300306 }
301307 else // no focus
302308 {
303- if ( _formulaPollingTimer != null )
304- _formulaPollingTimer . Change ( - 1 , - 1 ) ;
309+ _formulaPollingTimer ? . Change ( - 1 , - 1 ) ;
305310 }
306311 }
307312 else if ( _formulaPollingTimer != null )
308313 {
309- _formulaPollingTimer . Dispose ( ) ;
314+ _formulaPollingTimer ? . Dispose ( ) ;
310315 _formulaPollingTimer = null ;
311316 }
312317 }
@@ -425,27 +430,27 @@ void OnStateChanged(StateChangeEventArgs stateChangeEventArgs)
425430
426431 public void Dispose ( )
427432 {
428- if ( _formulaPollingTimer != null )
429- {
430- _formulaPollingTimer . Dispose ( ) ;
431- }
433+ Logger . WindowWatcher . Verbose ( "FormulaEdit Disposing" ) ;
434+ XlCall . ShutdownStarted ( ) ;
435+
432436 // Not sure we need this:
433437 _windowWatcher . FormulaBarWindowChanged -= _windowWatcher_FormulaBarWindowChanged ;
434438 _windowWatcher . InCellEditWindowChanged -= _windowWatcher_InCellEditWindowChanged ;
435439// _windowWatcher.MainWindowChanged -= _windowWatcher_MainWindowChanged;
436440
437- _syncContextAuto . Post ( delegate
441+ _formulaPollingTimer ? . Dispose ( ) ;
442+ _formulaPollingTimer = null ;
443+
444+ _syncContextAuto . Send ( delegate
438445 {
439- Debug . Print ( "Disposing FormulaEditWatcher" ) ;
440- //Automation.RemoveAutomationFocusChangedEventHandler(FocusChangedEventHandler);
441- //if (_formulaBar != null)
442- //{
443- // Automation.RemoveAutomationEventHandler(TextPattern.TextChangedEvent, _formulaBar, TextChanged);
444- // _formulaBar = null;
445- //}
446+ if ( _formulaBar != null )
447+ {
448+ UninstallTextChangeMonitor ( _formulaBar ) ;
449+ _formulaBar = null ;
450+ }
446451 if ( _inCellEdit != null )
447452 {
448- Automation . RemoveAutomationEventHandler ( TextPattern . TextChangedEvent , _inCellEdit , TextChanged ) ;
453+ UninstallTextChangeMonitor ( _inCellEdit ) ;
449454 _inCellEdit = null ;
450455 }
451456 //if (_mainWindow != null)
0 commit comments