private void AutoComplete() // in a thread.. { do { System.Diagnostics.Debug.WriteLine("{0} Begin AC", Environment.TickCount % 10000); restartautocomplete = false; autocompletestrings = new SortedSet <string>(); if (PerformAutoCompleteInThread != null) // first see if a thread wants action { System.Diagnostics.Debug.WriteLine("AC in thread"); PerformAutoCompleteInThread(string.Copy(autocompletestring), this, autocompletestrings); } if (PerformAutoCompleteInUIThread != null) // then see if the UI wants action { System.Diagnostics.Debug.WriteLine("Fire in ui"); autocompleteinuitimer.FireNow(); // fire a UI thread timer off AutocompleteUIDone.WaitOne(); // and stop thread until AutoCompleteInUI done System.Diagnostics.Debug.WriteLine("Fire in ui done"); } System.Diagnostics.Debug.WriteLine("{0} finish func ret {1} restart {2}", Environment.TickCount % 10000, autocompletestrings?.Count, restartautocomplete); } while (restartautocomplete == true); executingautocomplete = false; triggercomplete.FireNow(); // fire it immediately. Next timer call around will trigger in correct thread. This is thread safe. }
private void AutoCompleteInUI(PolledTimer t, long tick) // in UI thread, fired by autocompleteinui timer { System.Diagnostics.Debug.WriteLine("{0} Perform in UI ", tick); PerformAutoCompleteInUIThread.Invoke(string.Copy(autocompletestring), this, autocompletestrings); // we know its not null AutocompleteUIDone.Set(); }