WPF应用程序多线程策略所需的建议 [英] Advice needed for multi-threading strategy for WPF application

查看:78
本文介绍了WPF应用程序多线程策略所需的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个单一窗口的WPF应用程序 窗口中是一个列表项(当然,这些列表项仍保存在数据库中) 我需要定期启动一个后台任务,以从Atom提要中更新数据库.随着每个新项目添加到数据库中,UI中的列表也必须更新以反映这一点.我不希望此后台任务减慢UI速度,但同时需要与UI交互.

I'm building a single window WPF application In the window is a list items (which are persisted in a database of course) Periodically I need to start a background task that updates the database from an Atom feed. As each new item is added to the database, the list in the UI must also update to reflect this. I don't want this background task to slow down the UI but at the same time it needs to interact with the UI.

阅读了许多文章并看到了许多简单的示例,但我仍然不确定实现此目的的最佳方法.

Having read loads of articles and seen lots of simple examples, I am still unsure of the best way to implement this.

我想也许我能做的是:

在Window_Loaded事件上,创建DispatchTimer. 当Tick事件触发时,调用UpdateDb()方法. UpdateDB()将从Atom提要中获取项目并添加到数据库中.当我遍历每个项目时,我将调用另一种方法将列表重新绑定到数据库,以使其刷新". 完成所有任务后,重置DispatchTimer ??? (不确定是否可以/需要/完成).

On the Window_Loaded event, create a DispatchTimer. When the Tick event fires, call UpdateDb() method. UpdateDB() will get the items from the Atom feed and add to the database. As I iterate through each item I will call another method to rebind the list to the database so that it "refreshes". When all the tasks are finished reset the DispatchTimer ??? (not sure if this can / needs to be / done).

请记住,这是后台任务,因此用户可以同时使用UI.

Remember, this is background task so a user could be using the UI at the same time.

听起来如何?

谢谢.

推荐答案

这听起来不太理想,因为您正在UI线程上进行数据库连接.当Tick事件在DispatcherTimer上触发时,处理程序将在UI线程上执行.您需要最小化在此线程上进行的工作量以保持UI响应,并且绝对不应该在此线程上进行IO绑定工作.

This sounds suboptimal because you're doing database connectivity on the UI thread. When the Tick event fires on the DispatcherTimer, handlers will execute on the UI thread. You need to minimize the amount of work you do on this thread to keep the UI responsive, and you definitely shouldn't be doing IO-bound work on this thread.

我可能会有一个数据服务,负责更新数据库并在进行更改时引发事件.您的UI层可以附加到这些事件,并编组到UI线程以应用更改.要编组到UI线程,只需要调用

I would probably have a data service whose responsibility is to update the database and raise events as changes are made. Your UI layer can attach to these events and marshal to the UI thread to apply changes. To marshal to the UI thread, you just need to call Dispatcher.Invoke.

不管您采用哪种具体方法,关键是要在一个单独的线程上尽可能多地执行操作(包括任何数据库访问).尽可能晚地编组回UI线程,并在UI线程上做尽可能少的工作.

Regardless of your specific approach, the key is to do as much as you can (including any database access) on a separate thread. Marshal back to the UI thread as late as possible and do as little work as possible on the UI thread.

要注意的另一件事是WPF会自动为您封送标量值的更改.您只需要整理对集合的更改(添加/删除/替换项目).

One other thing to note is that WPF automatically marshals changes to scalar values for you. You only need to marshal changes to collections (adding/removing/replacing items).

这篇关于WPF应用程序多线程策略所需的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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