我试图阐明我对 Async/Await 的理解。 (我在这里阅读了很多关于 SO 的内容,但还没有弄清楚)。假设我有一个开始一个漫长过程的窗口构造函数:
// Constructor
public MainWindowViewModel() : base()
{
init();
........
}
private async void init()
{
PatientList = await GetPatientList(); <--- A VERY LONG PROCESS
.....
}
private ObservableCollection<ViewPatient> PatientList;
private async Task<ObservableCollection<ViewPatient>> GetPatientList()
{
return new ObservableCollection<ViewPatient>(await MedicalClient.GetAllPatientsAsync());
}
所以,如果我做对了,await 将启动 GetPatientList()方法,然后 立即返回给调用者(MainWindowViewModel() 然后将完成并显示窗口。
现在,在窗口中我有一个搜索列表中名称的按钮。快速跳转几次后,发现者调用:
private async Task<ObservableCollection<ViewPatient>> GetPatientListFromName(string lastname, string firstname, string birthdate)
{
string birth = string.Empty;
if (!string.IsNullOrWhiteSpace(birthdate))
{
// regex to look for pattern: 00/00/0000, 0/00/0000, 00/0/0000
Regex regex = new Regex(@"^\d{1,2}\/\d{1,2}\/\d{4}$");
Match x = regex.Match(birthdate);
if (!x.Success) return null;
birth = birthdate;
}
string last = lastname ?? string.Empty;
string first = firstname ?? string.Empty;
// PatientList = await GetPatientList(); <--Is This Needed?
var z = PatientList.Where(p =>
p.Lastname.StartsWith(last.ToUpper()) &&
p.Firstname.StartsWith(first.ToUpper()) &&
((DateTime)p.Birthdate).ToShortDateString().StartsWith(birth));
return new ObservableCollection<ViewPatient>(z);
}
所以现在我的问题是:
- 如果
GetPatientList()到PatientList时尚未完成(因此GetPatientListFromName(...)未知)需要它,将GetPatientListFromName(...)在继续进入 LINQ 表达式之前自动等待其完成?或者还需要什么? - 如果我包含(注释掉)
PatientList = await GetPatientList();将GetPatientList()重新启动——即使它已经在运行?
请您参考如下方法:
I'm trying to clarify my understanding of Async/Await.
我建议您从 my tutorial 开始,在底部有指向 (IMO) 最佳后续资源的链接。由于您正在编写 WPF 应用程序,因此我还推荐我关于异步 MVVM 的文章系列(尤其是 async data binding 上的文章)。 SO 非常适合问答,但它并不是真正的教程/学习网站。
关于 await 的第一件事是它如何拆分其 async 方法。特别是,这个:
private async void init()
{
PatientList = await GetPatientList();
}
本质上与此相同:
private async void init()
{
var task = GetPatientList();
var result = await task;
PatientList = result;
}
这应该清楚
GetPatientList在await开始之前被调用。PatientList在await完成后分配。
考虑到这一点,您可以回答您的问题:
If the GetPatientList() has not completed (so PatientList is not known) by the time GetPatientListFromName(...) needs it, will GetPatientListFromName(...) automatically wait for its completion prior to continuing into the linq expression?
没有。 PatientList 将为 null,直到 init 中的 await 完成。
If I include the (commented out) PatientList = await GetPatientList(); will GetPatientList() be started again --even if it is already running?
是的。这将第二次调用 GetPatientList,创建另一个任务。
要解决您的实际问题(即只加载一次 PatientList 但允许搜索按钮正常工作),您可以通过几种不同的方式来解决。
一种方法是保存“初始化”任务,如下所示:
private Task _initTask;
public MainWindowViewModel() : base()
{
_initTask = InitAsync();
...
}
private async Task InitAsync()
{
PatientList = await GetPatientList();
...
}
private async Task<ObservableCollection<ViewPatient>> GetPatientListFromName(string lastname, string firstname, string birthdate)
{
...
string last = lastname ?? string.Empty;
string first = firstname ?? string.Empty;
// Ensure PatientList is loaded.
await _initTask;
var z = PatientList.Where...
}
如果 Search 尚未完成,这将导致您的 Search 按钮(异步)等待 PatientList。 await 已经完成的任务以及多次 await 任务是绝对安全的。
这种方法的一个好处是 async void 被转换为 async Task。 It's a best practice to avoid async void .
