1

I have to render about 10'000 wpf controls, and I don't want to freeze my application during this action. Is it possible to do it in another thread?

I tried to use

    myStackPanel.Dispatcher.BeginInvoke(
    System.Windows.Threading.DispatcherPriority.SystemIdle, 
    new NextPrimeDelegate(this.AddButton));

and another variants of Dispatcher. But it always freeze my application. I'd like to show animated waiting screen during creating hundred controls, but it's always freeze, because it's dispatcher's thread.

thanks for help Andrew.

3
  • I dont think you can create controls on different thread. All WPF elements initialization should be on primary STA thread. But why are you creating that many controls? You should use some kind of UI virtualization for such scenario. Commented Apr 28, 2013 at 19:11
  • 3
    Please elaborate why you have to create 10.000 controls. Think there should be some alternative to that. Please also check this article if it is helpful for you: blogs.microsoft.co.il/blogs/tomershamam/archive/2009/09/06/… Commented Apr 28, 2013 at 19:12
  • @MareInfinitus, the link above is dead. Commented Aug 25, 2014 at 8:18

1 Answer 1

4

WPF controls must be created on the thread associated with the dispatcher where they will be presented. The call to Dispatcher.BeginInvoke schedules the specified action to run on the dispatcher thread at a later time, not on a background thread.

If you have a lot of UI controls to create and want to display an animation during their creation, then you will need to segment the work to be done into small units that can be quickly processed. You can then call BeginInvoke at a priority lower than normal to process each unit of work group individually.

The fact that you need to create 10,000 controls is extreme and you should probably investigate virtualizing the controls.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.