InsertEmployee
中的最后一项陈述是FetchEmployeesAsync
。但FetchEmployeesAsync
有一个await
。
据我所知,如果等待的任务没有完成,控制权将转移回呼叫者。但是执行仍然会在InsertEmployee
结束时等待,而不是去调用者。我是对的吗?
public void InsertEmployee(Employee e)
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
conn.InsertAsync(e);
FetchEmployeesAsync();
}
public async void FetchEmployeesAsync()
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
employees = await conn.Table<Employee>().ToListAsync();
DisplayList();
}
答案 0 :(得分:1)
由于conn.Table<Employee>
等待InsertEmployee
调用,它将在任务完成时将控制权返回给其调用者。由于此方案中的调用方为FetchEmployees
,无需等待即调用InsertEmployee
,因此InsertEmployee
方法将立即完成并退出。
当在堆栈中进一步等待任务时,控制将一直返回到聚合根,除非在到达聚合根的路上某个时候等待它,这通常是(但不总是)用户界面。
如果您想在FetchEmployees
完成之前阻止FetchEmployees
方法完成,可以让public async void InsertEmployee(Employee e)
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
conn.InsertAsync(e);
await FetchEmployees();
}
public async Task FetchEmployeesAsync()
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Employee.sqlite");
employees = await conn.Table<Employee>().ToListAsync();
DisplayList();
}
返回任务。
FetchEmployeesAsync
虽然Task
的返回值为<?php
$url = 'http://www.dandwiki.com/wiki/SRD:Classes';
$html = file_get_contents($url);
$dom = new DOMDocument();
//from file
@$dom->loadHTML($html);
//Creating a new DOMPath
$Xpath = new DOMXPath($dom);
// Get links
// //big Selects all <big> elements in the document.
// [text()="Base Classes"] with text "base classes"
// /.. get parent class
// /.. get parent class
// //a select all <a> elements
// /text() get text
$query = '//big[text()="Base Classes"]/../..//a/text()';
$entries = $Xpath->query($query);
foreach ($entries as $entry) {
echo $entry->nodeValue . "<br>";
}
?>
,但您不必明确返回一个。在编译过程中会发生一些神奇的事情,当等待发生时,会为您返回一个任务,其余的方法被封送到一个延续