【发布时间】:2015-09-22 12:53:25
【问题描述】:
我目前正在 WPF / MVVM 中遵循 dataservice 模式进行开发,其中 ViewModel 调用包含所有业务对象和方法的 Service .
现在,当我调用服务方法时,这需要一些时间,所以我应该创建一个新任务,以使 GUI 不会冻结。
在您看来,在 ViewModel 中还是在服务本身中启动任务的最佳位置是哪里?
...
// TaskFactory.StartNew(() => {}); // where I should put this ? *
...
class DataService
{
MyBussObj mbo;
CallBusinessOperation()
{
// * here ?
while (mbo.Next())
{
// requires a while
}
}
}
class MyViewModel
{
DataService service = new DataService();
void DoIt()
{
// * here ?
service.CallBusinessOperation();
}
}
【问题讨论】:
-
IMO 你应该使服务方法异步,并用 await 调用它们
-
因为它比单纯使用任务更清晰透明
-
如果你在Net 4.0你只能使用Task
标签: c# wpf mvvm viewmodel dataservice