问题描述
我希望能够在不导致UI延迟的情况下将项目快速添加到数据网格中。
I want to be able to add items to a Data grid at a fast rate without causing UI delay.
我现在正在做的是:
我正在使用绑定到数据网格的ObservableCollection。
Here is what I am doing now:I am using an ObservableCollection that is bound to the data grid.
我使用的后台线程仅在从可观察集合中插入/删除时循环并在当前调度程序上调用Invoke。相反,调用BeginInvoke会产生不良结果。
I use a background thread that loops and calls Invoke on the current dispatcher only when inserting/removing from the observable collection. Calling BeginInvoke instead has undesirable results.
我知道在调度程序上调用太多代码会导致延迟,但是我不知道该怎么办。我以前使用过后台工作人员,但我认为这不适用于我的情况。
I know that invoking that much on the dispatcher is causing the delay but I don't know what else to do. I have used background workers before but I don't think that applies to my scenario.
我该怎么做才能使UI保持响应状态?
What can I do to keep the UI responsive?
推荐答案
批处理更新-后台线程可以将项目添加到队列中,并且您可以通过调用定期刷新绑定的可观察集合。如果需要处理多线程生产者,请查看System.Collections.Concurrent命名空间。
Batch the updates -- the background thread could add items to a queue and you can periodically refresh your bound observable collection by invocation. Take a look at the System.Collections.Concurrent namespace if you need to handle multi-threaded producers
这篇关于数据网格绑定(WPF)导致UI延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!