我有一个实时应用程序,它以 ListView 控件的形式不断更新 UI 日志(RichTextBox 控件)。该控件使用通过事件接收的当前应用程序数据进行更新。我的应用程序运行速度非常慢,我发现这是由于 ListView 日志正在更新,这会阻塞 UI 线程。正如您可以想象的那样,该应用程序对用户来说似乎反应迟钝。
我知道可以在其自己的专用 UI 线程上启动 WPF 窗口。我想知道是否可以在其自己的 UI 线程上托管 WPF 控件,以便主 UI 线程更新窗口的其余部分而不被阻止?
如果这是不可能的,请推荐替代方案来解决这个困境。
谢谢!
I have a real-time application which constantly updates a UI log (RichTextBox control) in the form of a ListView control. The control is updated with current application data received via events. My application was running very slow and i found it was due to the ListView log being updated, which blocks the UI thread. As you can imagine, the application appears highly unresponsive to the user.
I know it's possible to launch a WPF Window on it's own dedicated UI thread. I was wondering if it's possible to host a WPF Control on it's own UI thread so that the main UI thread updates the rest of the window without being blocked?
If this is not possible, please recommend alternatives to remedy this dilemma.
Thanks!
发布评论
评论(2)
您尚未发布任何代码,但为什么不将
ListView
绑定到ObservableCollection
?您更新集合,它会引发一个事件来告诉 UI 它已更改 - 这将更新 UI。有很多关于如何执行此操作的教程 - 可以在 打开代码
You haven't posted any code, but why don't you bind the
ListView
to anObservableCollection
? You update the collection and it raises an event to tell the UI that it's changed - it's this that will update the UI.There's many tutorials on how to do this - one such can be found on Switch On The Code
RichTextBox 是一个需要定期更新的昂贵控件。
无法在其自己的 UI 线程上承载 WPF 控件。
ObservableCollection 在我的情况下不起作用,因为我无法直接访问我绑定的对象 - 第三方对象。
我的解决方案(需要最少的更改)是将 UI Log 控件基于 ListView。 ListView 已针对批量和定期更新进行了优化 - 得益于 VirtualizedStackPanel 标签。
RichTextBox is an expensive control to update regularly.
It is not possible to host a WPF control on it's own UI thread.
ObservableCollection will not work in my case, as i do not have direct access to the objects that i am binding - 3rd party objects.
My solution, which requires minimal changes, was to base the UI Log control on a ListView. ListView has been optimised for bulk and regular updates - thanks to VirtualisedStackPanel tag.