具有listview的WPF应用程序挂起并且线程不断增加 [英] WPF application with listview hangs and threads keep increasing

查看:180
本文介绍了具有listview的WPF应用程序挂起并且线程不断增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,
有一个很奇怪的问题,我将不得不在背景上做一些解释.

我正在处理一个WPF客户端应用程序,该应用程序已加载约3-4个用户控件,每个控件都有一个列表视图,使用可观察的集合显示记录.此可观察的集合是遵循INotifyPropertyChanged模型的类实例的集合.集合中的每次更改后,更改都会反映到listview列上.使用WCF TCP/IP可以以每秒约100-200个更新的速度从服务器中继续对集合中现有行的更新.

问题是,UI有时会冻结,然后连接到进程的线程数(在任务管理器中看到)不断增加..我不知道为什么会发生这种情况.当我调试更新仍在进行并且可观察的集合仍在更新时..但是UI挂起..这并不总是发生或在特定操作上发生.

我在添加项目时使用Dispatcher.BeginInvoke到列表视图,但在更新集合中实例的属性时则不行,因为可观察的集合会自动处理此类更新.我还在列表视图中使用虚拟化,但默认情况下该虚拟化始终处于启用状态.

任何线索可能导致此问题..

Hi,
There is a weird problem for which I will have to do some explanation on background.

I am working on a WPF client application which has about 3-4 usercontrols loaded, each having a listview displaying records using a observable collection. This observable collection is a collection of class instances that follow the INotifyPropertyChanged model. Upon each change in the collection the change reflects onto the listview column. Updates to existing rows in the collection keep streming in from the server using WCF TCP/IP at about 100-200 updates per second.

The issue is that once in a while the UI just freezes and then the no of threads attached to the process (seen in the task manager) keep on increasing exponentially.. I have no clue why this happens.. when i debug the updates are still coming in and observable collection is still getting updated.. but the UI hangs.. This does not happen always or on a particular action.

I am using Dispatcher.BeginInvoke when adding items to the listview but not when updating the properies of instances within the collection as observable collection automatically takes care of such updates. I am also using Virtualization in listview which is anyways enabled by default.

Any clues what could be causing this..

推荐答案

建议,
-您的描述唯一令人惊讶的是,您的对象实现了INotifyPropertyChanged. ObservableCollection本质上实现了更改通知.您可能想尝试禁用对象的属性更改机制,而仅依赖于ObservableCollection.
-您为BeginInvoke设置了什么DispatcherPriority?您是否尝试将其设置为较低的优先级,例如背景?
-不更新主要的Observable集合.尝试另一种,并用更新后的替代品定期替换主要的替代品.我对此并不满意:>
Suggestions,
- The only thing striking from your description is, your object implements INotifyPropertyChanged. ObservableCollection inherently implement the change notification. You might want to try disabling the property change mechanism of the objects and just rely on the ObservableCollection.
- What DispatcherPriority do you set for BeginInvoke ? Did you try setting it to a lower priority e.g. Background ?
- Don''t update the main Observable collection. Work on a different one and periodically replace the main one with the updated one. I don''t feel good about this one though :>


问题似乎出在wpf框架上,尽管我不确定.我在以下线程上找到了解决我问题的方法,这使您可以很好地阅读.

尝试使用此链接,并在稍后查看Kevin Kerr评论一半的线程 [
The issue seems to be with the wpf framework, although i am not sure. I found the solution to my problem on the following thread, which makes for a good reading.

Try this link and check out Kevin Kerr comment in later half of the thread[^]

Now i let the background thread do the updations as fast as it wants to without raising the property changed events at that time, but i raise all the property changed events for all objects in collection from the UI thread on every 100 frames using the CompositionTarget.Rendering event. Now the hang problem is thankfully put to rest!


这篇关于具有listview的WPF应用程序挂起并且线程不断增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆