Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load an ObservableCollection<string> with a Task

I am trying to load all the users in our active directory and display them in a ListBox. However if I do this like normal I freeze the UI thread for a long time. So is there anyway I can use a task to fill this collection up on a background thread while still getting the listbox to update as I insert new names?

like image 668
twreid Avatar asked Aug 12 '26 02:08

twreid


1 Answers

As you cannot load all the data in a separate thread (or task, whatever) and then fill the ObservableCollection, you can pass the current Dispatcher to the operation and use its InvokeAsync method to add the elements one by one to the Observable collection in the UI thread. Something like this:

public void FetchAndLoad()
    {
        // Called from the UI, run in the ThreadPool
        Task.Factory.StartNew( () =>
        this.FetchAsync(e => this.Dispatcher.InvokeAsync(
            () => this.observableCollection.Add(e)
            )
        ));
    }

    public void Fetch(Action<string> addDelegate)
    {
                    // Dummy operation
        var list = new List<string>("Element1", "Element2");

        foreach (var item in list)
            addDelegate(item);
    }

I would do that in batches, though, depending on the number of elements.

like image 76
Arthur Nunes Avatar answered Aug 14 '26 17:08

Arthur Nunes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!