@@ -61,11 +61,13 @@ void RunUIAutomation()
6161 {
6262 _syncContextAuto = new SingleThreadSynchronizationContext ( ) ;
6363
64+ // Create and hook together the various watchers
6465 _windowWatcher = new WindowWatcher ( _syncContextAuto ) ;
6566 _formulaEditWatcher = new FormulaEditWatcher ( _windowWatcher , _syncContextAuto ) ;
6667 _popupListWatcher = new PopupListWatcher ( _windowWatcher , _syncContextAuto ) ;
6768 // _selectDataSourceWatcher = new SelectDataSourceWatcher(_windowWatcher, _syncContextAuto);
6869
70+ // These are the events we're interested in for showing, hiding and updating the IntelliSense forms
6971 _windowWatcher . MainWindowChanged += _windowWatcher_MainWindowChanged ;
7072 _popupListWatcher . SelectedItemChanged += _popupListWatcher_SelectedItemChanged ;
7173 _formulaEditWatcher . StateChanged += _formulaEditWatcher_StateChanged ;
@@ -75,30 +77,36 @@ void RunUIAutomation()
7577 _syncContextAuto . RunOnCurrentThread ( ) ;
7678 }
7779
78- // Runs on the auto thread
80+ #region Receive watcher events, and post to main thread
81+ // Runs on our automation thread
7982 void _windowWatcher_MainWindowChanged ( object sender , EventArgs args )
8083 {
84+ Logger . Display . Verbose ( "! MainWindowChanged" ) ;
8185 _syncContextMain . Post ( MainWindowChanged , null ) ;
8286 }
8387
84- // Runs on the auto thread
88+ // Runs on our automation thread
8589 void _popupListWatcher_SelectedItemChanged ( object sender , EventArgs args )
8690 {
91+ Logger . Display . Verbose ( "! PopupList SelectedItemChanged" ) ;
8792 _syncContextMain . Post ( PopupListSelectedItemChanged , null ) ;
8893 }
8994
90- // Runs on the auto thread
95+ // Runs on our automation thread
9196 void _formulaEditWatcher_StateChanged ( object sender , FormulaEditWatcher . StateChangeEventArgs args )
9297 {
98+ Logger . Display . Verbose ( $ "! FormulaEdit StateChanged ({ args . StateChangeType } )") ;
9399 _syncContextMain . Post ( FormulaEditStateChanged , args . StateChangeType ) ;
94100 }
101+ #endregion
95102
96103 // Runs on the main thread
97104 void MainWindowChanged ( object _unused_ )
98105 {
99106 // TODO: This is to guard against shutdown, but we should not have a race here
100107 // - shutdown should be on the main thread, as is this event handler.
101- if ( _windowWatcher == null ) return ;
108+ if ( _windowWatcher == null )
109+ return ;
102110
103111 // TODO: !!! Reset / re-parent ToolTipWindows
104112 Debug . Print ( $ "IntelliSenseDisplay - MainWindowChanged - New window - { _windowWatcher . MainWindow : X} , Thread { Thread . CurrentThread . ManagedThreadId } ") ;
@@ -128,7 +136,8 @@ void PopupListSelectedItemChanged(object _unused_)
128136 {
129137 Debug . Print ( $ "IntelliSenseDisplay - PopupListSelectedItemChanged - New text - { _popupListWatcher ? . SelectedItemText } , Thread { Thread . CurrentThread . ManagedThreadId } ") ;
130138
131- if ( _popupListWatcher == null ) return ;
139+ if ( _popupListWatcher == null )
140+ return ;
132141 string functionName = _popupListWatcher . SelectedItemText ;
133142
134143 IntelliSenseFunctionInfo functionInfo ;
@@ -162,7 +171,8 @@ void FormulaEditStateChanged(object stateChangeTypeObj)
162171 var stateChangeType = ( FormulaEditWatcher . StateChangeType ) stateChangeTypeObj ;
163172 // Check for watcher already disposed
164173 // CONSIDER: How to manage threading with disposal...?
165- if ( _formulaEditWatcher == null ) return ;
174+ if ( _formulaEditWatcher == null )
175+ return ;
166176
167177 if ( stateChangeType == FormulaEditWatcher . StateChangeType . Move && _argumentsToolTip != null )
168178 {
@@ -305,7 +315,8 @@ public void Dispose()
305315 if ( _syncContextAuto == null )
306316 return ;
307317
308- _syncContextAuto . Send ( delegate
318+ // Send is not supported on _syncContextAuto
319+ _syncContextAuto . Post ( delegate
309320 {
310321 if ( _windowWatcher != null )
311322 {
@@ -325,6 +336,8 @@ public void Dispose()
325336 _popupListWatcher . Dispose ( ) ;
326337 _popupListWatcher = null ;
327338 }
339+ _syncContextAuto . Complete ( ) ;
340+ _syncContextAuto = null ;
328341 } , null ) ;
329342
330343 _syncContextMain . Send ( delegate
@@ -341,10 +354,6 @@ public void Dispose()
341354 }
342355 } , null ) ;
343356
344- _syncContextAuto . Complete ( ) ;
345- // CONSIDER: Maybe wait for the _syncContextAuto to finish...?
346- _syncContextAuto = null ;
347-
348357 _syncContextMain = null ;
349358 }
350359
0 commit comments