RealityDysfunction
RealityDysfunction

Reputation: 2639

async causes debugger jump

I have this code:

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

I set the breakpoint on the first highlighted line (which it hits), then I press F10, and debugger jumps to the last bracket! I am not understanding why.

GetData method:

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


}

Upvotes: 4

Views: 1443

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70691

Because that's what "await" does. When you use "await" in an async method, it tells the compiler that you want the method to return at that point, and then to re-enter the method later only when the "awaited" task has completed.

Upvotes: 7

Related Questions