Autocomplete text field freezes during query execution. There must be a better way!

all! I was looking for the best I could, and did not find exactly what help I was looking for.

Problem

AutoCompleteTextbox FREEZES and "eats" characters at query time

Inquiry

Google Instant Expression Feature

Background

First things: C #, WPF, .NET 4.0

Well, now, aside, I'm trying to find a better way to implement dynamic autocomplete text field, which queries the database for the results after each letter entered.

The following code is fired on an AutoCompleteTextBox TextChanged event:

    public void Execute(object sender, object parameter)
    {
        //removed some unnecessary code for the sake of being concise

        var autoCompleteBox = sender as AutoCompleteTextBox;
        var e = parameter as SearchTextEventArgs;

        var result = SearchUnderlyings(e.SearchText);

        autoCompleteBox.ItemsSource = result;
    }

, SearchUnderlyings(e.SearchText) 600-1100 - "" . , . - LINQ SearchUnderlyings(e.SearchText) . , .

, , Google Instant, "" , / .

- , GUI ?

, !

+5
3

:

var result = SearchUnderlyings(e.SearchText);

, . , , , - , .

- http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

+4

, , ( ).

. , , . . , , , . , ( ).

, .

+2

, - . SearchUnderlyings (e.SearchText) , "" . lastQueryTag == _lastQuery, , ItemsSource.

Perhaps this is not the most ideal or elegant solution. I am still open to further criticisms and suggestions. Thank!

private long _lastQuery = DateTime.Now.Ticks;

public void Execute(object sender, object parameter)
{
    var autoCompleteBox = sender as AutoCompleteTextBox;
    var e = parameter as SearchTextEventArgs;

    // removed unecessary code for clarity

    long lastQueryTag = _lastQuery = DateTime.Now.Ticks;
    Task.Factory.StartNew(() =>
    {                        
        var result = SearchUnderlyings(e.SearchText);

         System.Windows.Application.Current.Dispatch(() =>
         {
             if (lastQueryTag == _lastQuery)
                  autoCompleteBox.ItemsSource = result;
         });
    });
}
+1
source

All Articles