後臺工作流程: 我有一個客戶端(jquery/ajax html頁面)調用我們的RESTful WebAPI來獲取一些數據(Patient'遭遇') - 例如進入醫院,訪問診所等)。例如在RESTful WebApi請求上運行後臺線程
public async Task<string> GetEncounters(string patientId)
{
PatientChart patientChart = await _myService.GetPatientChart(patientId);
string message = string.Empty;
if (patientChart.Encounters.Status == PatientChart.StatusNotApplicable)
{
message = "List of Encounters is not available. A request has been made to retrieve it.";
_myService.GetEncounters(patientId); // async method without call to await
}
return message;
}
問題 會發生什麼上面不施加了的await關鍵字「GetEncounters」的號召? 從我的理解,異步方法不會生成一個新的線程 所以當主線程死亡,這是否意味着調用GetEncounters將中止? (在幕後,GetEncounters會觸發長時間運行的進程來獲取數據並將其存儲在緩存中,以備以後檢索)。
如果我逐句通過代碼,每一個都按預期執行。同樣,如果我添加await關鍵字,它也可以。 (但後來我打來電者)
什麼是解決方案? 即使主線程已經死亡,創建後臺任務/線程來執行代碼的最佳方式是什麼?