异步导致调试器跳转

时间:2014-10-21 04:44:37

标签: 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);
**}**

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

GetData方法:

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


}

1 个答案:

答案 0 :(得分:5)

因为那是&#34;等待&#34;确实。当你使用&#34; await&#34;在异步方法中,它告诉编译器您希望该方法在该点返回,然后仅在等待&#34;等待的时候重新输入该方法。任务已经完成。