简体   繁体   English

在后台线程中创建wpf控件

[英]Creating wpf controls in background thread

I have to render about 10'000 wpf controls, and I don't want to freeze my application during this action. 我必须渲染大约10'000个wpf控件,我不想在此操作期间冻结我的应用程序。 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. 和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. 我想在创建100个控件期间显示动画等待屏幕,但它总是冻结,因为它是调度程序的线程。

thanks for help Andrew. 谢谢你的帮助安德鲁。

WPF controls must be created on the thread associated with the dispatcher where they will be presented. 必须在与调度程序关联的线程上创建WPF控件。 The call to Dispatcher.BeginInvoke schedules the specified action to run on the dispatcher thread at a later time, not on a background thread. Dispatcher.BeginInvoke的调用会Dispatcher.BeginInvoke指定的操作以便稍后在调度程序线程上运行,而不是在后台线程上运行。

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. 如果要创建许多UI控件并希望在创建过程中显示动画,则需要将要完成的工作分段为可以快速处理的小单元。 You can then call BeginInvoke at a priority lower than normal to process each unit of work group individually. 然后,您可以以低于正常的优先级调用BeginInvoke ,以单独处理每个工作单元。

The fact that you need to create 10,000 controls is extreme and you should probably investigate virtualizing the controls. 您需要创建10,000个控件的事实是极端的,您应该调查虚拟化控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM