How can I add many UserControls to StackPanel without freeze?

0

i try to develop a equivalent to a android app (just for fun / 2 learn) and i try to add round about 3500 UserControls to my GUI in a StackPanel.

One UserControl contain 2 textblocks (titel and subtitle), two buttons and one image.

If i add the controls in the UI thread the thread will be blocked for round about 6 sec. But an asynchronous thread will throw an exception:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

            var task = Task.Run(async () =>
        {
            MyUserControl1 News = new MyUserControl1();

            // EXAMPLE
            for (int i = 0; i < 3500; i++)
            {
                News= new MyUserControl1();

                News.Titel = i + " Titel";
                News.Untertitel = i + " Untertitel";

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    UserControlCollection.Children.Add(News);
                });
            }

        });

What can I do to resolve this fault? thx for your help

c#
multithreading
user-controls
async-await
uwp
asked on Stack Overflow Dec 22, 2016 by Paule Paul • edited Dec 22, 2016 by Uwe Keim

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0