Unknown Coder
Unknown Coder

Reputation: 6731

Autocomplete Box - Inconsistent values from population

I have a WPF application with the autocomplete box via the toolkit (VS 2008). I have a potential population of about 2000 records and I have tried to improve performance with a combination of the populating event procedure. I am getting inconsistent results. The filter seems to be OK but I can run the app once and result X will be there but result Y wont. Running it again can make result Y be there and not X, subsequent times both X and Y will be there, etc, etc. This is my first time using the autocomplete box so I'm sure it must be something in my code that I'm forgetting. If I check my result set just prior to the Itemsource binding, the desired results are there, but they are not made visible to the user - the drop-down autocomplete back does not show. Maybe I need an event override???

The XAML

<input:AutoCompleteBox                         
Name="autGlobal"
FilterMode="Contains"
Style="{DynamicResource MiniSearchAutoBoxWPF}"
IsTextCompletionEnabled="false" 
Margin="5, 0, 5, 0" 
HorizontalAlignment="Center"
KeyUp="autGlobal_KeyUp"
Text="Search Term" 
GotFocus="autGlobal_GotFocus"
ValueMemberPath="Item" 
Populating="AutoCompleteBox_Populating"
>

The Methods

 private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e)
            {
            e.Cancel = true;
            var b = new BackgroundWorker();
            currSearch = autGlobal.Text;
            b.DoWork += b_DoWork;
            b.RunWorkerCompleted += b_RunWorkerCompleted;
            b.RunWorkerAsync(autGlobal.Text);
        }

private void b_DoWork(object sender, DoWorkEventArgs e)
        {
            Results.Clear();
            int counter = 0;
            string search = e.Argument.ToString();
            search = search.ToUpper();
            foreach (GlobalSearchList person in myGlobalList)
            {
                if (person.Item.ToUpper().Contains(search))
                {
                    Results.Add(person);
                    counter++;

                    if (counter >= MAX_NUM_OF_RESULTS)
                    {                        
                        break;
                    }
                }
            }
        }

private void b_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (this.Dispatcher.Thread == System.Threading.Thread.CurrentThread)
            {
                //Set the source
                if (currSearch == autGlobal.Text)
                {
                    autGlobal.ItemsSource = Results;
                    autGlobal.PopulateComplete();
                }
            }
            else
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    //Set the source
                    if (currSearch == autGlobal.Text)
                    {
                        autGlobal.ItemsSource = Results;
                        autGlobal.PopulateComplete();
                    }

                }));
            }            
        }

Upvotes: 3

Views: 560

Answers (2)

Ariel
Ariel

Reputation: 1661

I'm not sure why you need the performance boost in the first place, you're trying to calculate the elements that should be in the Autocomplete box in another thread and then assign them to the ItemsSource property of the Control. Something similar is what the AutoCompleteBox should do.

I tryed bind the control to a list with 10000 strings and it works perfect, so your problem could be the size of the objects that you're putting in the collection. One solution could be use just a string representation and then when you need the selected object you could find it based on it's representation, assuming that is unique (if not you could put some sort of ID).

One of the main problems with this approach is the thread sincronization, i will explain now why you get the extrange behavior where even when the filter is fine the items in the results are not right.

The filter seems to be OK but I can run the app once and result X will be there but result Y wont. Running it again can make result Y be there and not X, subsequent times both X and Y will be there, etc, etc.

Suppose that you write "ab" in the autocomplete box, this will start a new BackGroundWorker where this search is performed. Everything should be fine if you wait long enough. But if you change the search query before the first worker has finished, now all the results will be mixed. Take for example the following lines of code:

// the user searchs for "ab"
[Thread 1] Results.Clear();
[Thread 1] Results.Add(Item[1]);
[Thread 1] Results.Add(Item[2]);
...
// the user changes the search term (to "abc" for example)
[Thread 2] Results.Clear();
[Thread 2] Results.Add(Item[3]);
// but what would happen if the first BackGroundWorker hasn't finished yet,
// this means that the first thread is still running
[Thread 1] Results.Add(Item[5]); // this items doesn't match the second search
[Thread 1] Results.Add(Item[6]); // criteria, but are added to the collection
[Thread 2] Results.Add(Item[7]);
// then you'll have two treads adding items to the Result collection
[Thread 1] Results.Add(Item[2]);
...
[Dispatcher Thread] autGlobal.ItemsSource = Results;
[Dispatcher Thread] autGlobal.PopulateComplete();

Hope this helps.

Upvotes: 3

Wilko de Zeeuw
Wilko de Zeeuw

Reputation: 21

Maybe you could check this one. All the work done for you.

http://gallery.expression.microsoft.com/WPFAutoCompleteBox/

Upvotes: 0

Related Questions