视图模型调用中的调用方法需要/消耗大量时间
嗨,我尝试解决这个情况。我有采用 MVVM 设计的 WPF 应用程序。我使用 Caliburn Micro 框架并注入 MEF。
在 WPF 应用程序中,我使用外部程序集的服务。效果很好。
问题是。我将可观察字典绑定到列表框。列表框可以包含 0 到 400 个项目。 我在列表框项目上有数据模板,它由图像和 som 文本框组成。列表框就像 Skype 或 google talk 中的联系人列表。
我每隔 3-4 秒从服务中调用一次方法,它将新数据作为字典返回。使用此数据刷新列表框。
我的代码在视图模型中看起来像这样:
private DispatcherTimer _dispatcherTimer;
private MyObservableDictionary<string, UserInfo> _friends;
//temp
private MyObservableDictionary<string, UserInfo> _freshFriends;
//bind on listbox
public MyObservableDictionary<string, UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
//in constructor of view model I have this:
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += DispatcherTimer_Tick;
_dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
_dispatcherTimer.Start();
// on timer tick I call method from service
private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
{
//get new data from server
//method GetFriends take much of time
_freshFriends = _service.GetFriends(Account);
//delete old data
_friends.Clear();
//refresh
foreach (var freshFriend in _freshFriends)
{
Friends.Add(freshFriend);
}
}
正如我所说,问题是服务中的 GetFriends 方法需要花费大量时间,并且我的应用程序会冻结。
如何解决这个问题呢?在 winforms 应用程序中,我使用后台工作程序,但这是我的第一个使用 MVVM 的 WPF 应用程序。在视图模型类中是否存在任何“模式”或“设计”如何调用消耗大量时间的方法?在另一个线程中调用这个方法?
Hi I try solve this situation. I have WPF app with MVVM design. I use Caliburn Micro framework and on injection MEF.
In WPF app I use service from external assembly. It works good.
Problem is. I bind observable dictionary to listbox. Listbox can consist from 0 to 400 items.
I have data template on listbox item it consist with image and som texbox. Listbox is like
contact list in skype or google talk.
I call every 3-4 sec method from service, wich returns new data as dictionary. An with this data aj refresh Listbox.
My code look in view model like this:
private DispatcherTimer _dispatcherTimer;
private MyObservableDictionary<string, UserInfo> _friends;
//temp
private MyObservableDictionary<string, UserInfo> _freshFriends;
//bind on listbox
public MyObservableDictionary<string, UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
//in constructor of view model I have this:
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += DispatcherTimer_Tick;
_dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
_dispatcherTimer.Start();
// on timer tick I call method from service
private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
{
//get new data from server
//method GetFriends take much of time
_freshFriends = _service.GetFriends(Account);
//delete old data
_friends.Clear();
//refresh
foreach (var freshFriend in _freshFriends)
{
Friends.Add(freshFriend);
}
}
As I said, problem is that method GetFriends from service take much of time and my app freezes.
How can solve this problem? In winforms app I use background worker, but this is my first WPF app with MVVM. It exist any "patern" or "design" how call method which consume much of time in view model class? Call this method in another thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他人所建议的,您可以在 WPF 应用程序中使用
BackgroundWorker
,或者如果您使用的是 .NET 4,则使用 任务并行库。 Stephen Cleary 在 TPL 上发表了一篇与BackgroundWorker
相比的精彩文章 - http://nitoprograms.blogspot.com/2010/06/reporting-progress-from-tasks.htmlAs others have suggested, you can use a
BackgroundWorker
in a WPF app, or if you are using .NET 4, then use the Task Parallel Library. Stephen Cleary has a nice post on the TPL compared toBackgroundWorker
here - http://nitoprograms.blogspot.com/2010/06/reporting-progress-from-tasks.html