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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With