Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;


}
like image 990
RealityDysfunction Avatar asked Mar 19 '23 03:03

RealityDysfunction


1 Answers

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.

like image 136
Peter Duniho Avatar answered Mar 27 '23 13:03

Peter Duniho