Marc
Marc

Reputation: 103

Adding controls to WPF windows without blocking GUI thread

I am trying to find a way to add multiple (100+ with high amount of data) controls to a WPF GUI without blocking the GUI thread by itself. That's how i create the controls currently:

I create the controls asynchronous in a parallel thread and i am able to add them to the GUI but when it comes to container.Children.Add() the GUI is blocked.

My first try was creating them by a task in a async method..

var a = Task.Factory.StartNew(() =>
{
    foreach (UserElement element in userElements)
        this.Dispatcher.Invoke(() => { UserElementsContainer.Children.Add(element); });
});
await a; //Won't work with or without await.

Is there another way?

Upvotes: 2

Views: 1059

Answers (1)

toadflakz
toadflakz

Reputation: 7944

You should rather use virtualisation and load controls as you need to display them - this way you reduce the amount of time required to load the controls as you are only showing a few at a time.

Check out this example which demonstrates a virtualised Canvas control. Microsoft Virtualized WPF Canvas

And also this other post for more virtualization technology references: Resources and guides to UI virtualization in WPF

Upvotes: 4

Related Questions