异步导致调试器跳转

Rea*_*ion 1 c# asynchronous

我有这个代码:

private async Task<DataSharedTheatres.TheatresPayload> GetTheatres()
{
    var callMgr = new ApiCallsManager();
    var fileMgr = new FileSystemManager();

    string cachedTheatres = fileMgr.ReadFile(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TheatreTemp.txt"));
    if (string.IsNullOrEmpty(cachedTheatres))
    {
        **string generalModelPull = await callMgr.GetData(new Uri("somecrazyapi.com/api" + apiAccessKey));**
        bool saveResult = fileMgr.WriteToFile(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TheatreTemp.txt"), generalModelPull);
        if (!saveResult)
        {
            testText.Text = "Failed to load Theatre Data";
            return null;
        }
        cachedTheatres = fileMgr.ReadFile(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TheatreTemp.txt"));
    }
    return Newtonsoft.Json.JsonConvert.DeserializeObject<DataSharedTheatres.TheatresPayload>(cachedTheatres);
**}**
Run Code Online (Sandbox Code Playgroud)

我在第一个突出显示的行(它命中)上设置了断点,然后按F10,调试器跳转到最后一个括号!我不明白为什么.

GetData方法:

public async Task<string> GetData(Uri source)
{
    if (client.IsBusy) 
        client.CancelAsync ();
    string result = await client.DownloadStringTaskAsync (source);
    return result;


}
Run Code Online (Sandbox Code Playgroud)

Pet*_*iho 5

因为这就是"等待"的作用.当您在异步方法中使用"await"时,它会告诉编译器您希望该方法在该点返回,然后仅在"等待"任务完成时才重新输入该方法.