0

I have a screen that retrieves information from a remote server and displays that information to the user. I would like this information to be updated when the screen is displayed (requiring no additional user interaction).

The code I ended up with looks like this:

protected override void OnAppearing()
{
    base.OnAppearing();
    Task task = UpdateField();
}
protected async Task UpdateField()
{
    try { MyLabel.Text = (await GoGetTheData ()).Stuff; }
    catch(Exception ) { MyLabel.Text = "Nope"; }
}

In this case, since I'm catching all exceptions in UpdateField(), is there any downside/danger to storing the Task into a local variable and then ignoring it?

Betty Crokker
  • 3,001
  • 6
  • 34
  • 68
  • It is still (based on your other Q - http://stackoverflow.com/questions/42701673/c-sharp-xamarin-no-win-situation-async-void?noredirect=1#42701673) very confusing why you believe you must return `Task` - standard pattern http://stackoverflow.com/questions/12803012/fire-and-forget-with-async-vs-old-async-delegate is `async void`. – Alexei Levenkov Mar 09 '17 at 18:17
  • There's so much discussion about the evils of "async void" and the throwing of exceptions causing the crash of the application, I'm trying to avoid it on general principle. It looks like in this case, async/await is not going to be as easy to read as doing a Task.Run() – Betty Crokker Mar 09 '17 at 18:21
  • If the return value doesn't mean anything to you you could also do this: `Task _ = UpdateField();` – pinedax Mar 09 '17 at 18:30
  • Should be as simple as calling `Task.Run(TaskName)` for a "Fire and Forget" Task inside your `OnAppearing`. If you need a result of the `Task`, then you would `await` a call to return that `Task`. – Jon Douglas Mar 09 '17 at 19:22
  • The way your code is written currently, you gain no benefit from TAP pattern as there are no other actions to be performed while you 'await' (your Task). – DaniDev Mar 09 '17 at 19:33

0 Answers0