apimash / StarterKits

APIMASH Starter Kits for Windows demonstrate the use and implementation of public Web Service API's such as Edmunds, Tom-Tom, Twitter, Tumblr, Yelp, Meetup and many others.
52 stars 31 forks source link

APIMASH-TomTom_BingMaps #2

Closed jwooley closed 11 years ago

jwooley commented 11 years ago

Task returning API methods are not awaited in UI code. Check the compile warnings to identify and resolve. Current warnings are showing as -

D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\LeftPanel.xaml.cs(235,17,235,25): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(107,17,107,41): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(138,17,138,42): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(172,29,172,43): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(198,21,198,35): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(242,17,242,67): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(248,48,248,111): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

jimoneil commented 11 years ago

Jim, none of those calls need to be awaited, hence the warning that you’re seeing. I was torn between adding an unnecessary await or dealing with the warning. Perhaps I should reconsider?

Jim O’Neil

Sent from Windows Mail

From: Jim Wooley Sent: ‎June‎ ‎8‎, ‎2013 ‎11‎:‎17‎ ‎AM To: apimash/StarterKits Subject: [StarterKits] APIMASH-TomTom_BingMaps (#2)

Task returning API methods are not awaited in UI code. Check the compile warnings to identify and resolve. Current warnings are showing as -

D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\LeftPanel.xaml.cs(235,17,235,25): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(107,17,107,41): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(138,17,138,42): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(172,29,172,43): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(198,21,198,35): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(242,17,242,67): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. 2>D:\Projects\StarterKits\APIMASH_TomTom_BingMaps_StarterKit\APIMASH-TomTom_BingMaps-StarterKit\MainPage.xaml.cs(248,48,248,111): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

— Reply to this email directly or view it on GitHub.

bhitney commented 11 years ago

I think it's ok to leave them un-awaited, provided the behavior is intentional. It would be odd to have an async method with a call that is not awaited, but there are legitimate cases if there are multiple async calls and only some require completion before execution continues.

tawalke commented 11 years ago

I would only add that I don’t think it’s weird to return have an async call without an await especially if your goal is to return a pending async function/method. This would be similar to returning a promise from a function in JS then executing when desired. An example of this in C# would be when you need async calls to return in a certain order or to prevent reentrancy issues. ex. Task.WhenAll (pending async functions)

Thanks, TEW

Tara E. Walker Technical Evangelist: Developer/Software Engineering developer & platform evangelism Southeast | Microsoft Corporation tara.walker@microsoft.commailto:tara.walker@microsoft.com | 770.315.1009 Blog: http://blogs.msdn.com/tarawalker/ Twitter: @taraw

"There are seven sins in the world: wealth without work, pleasure without conscience, knowledge without character, commerce without morality, science without humanity, worship without sacrifice, and politics without principle."

From: bhitney [mailto:notifications@github.com] Sent: Saturday, June 8, 2013 4:03 PM To: apimash/StarterKits Subject: Re: [StarterKits] APIMASH-TomTom_BingMaps (#2)

I think it's ok to leave them un-awaited, provided the behavior is intentional. It would be odd to have an async method with a call that is not awaited, but there are legitimate cases if there are multiple async calls and only some require completion before execution continues.

— Reply to this email directly or view it on GitHubhttps://github.com/apimash/StarterKits/issues/2#issuecomment-19154579.

jwooley commented 11 years ago

Sorry, I meant to reply previously. I was reminded about this from a Stack Overflow question (http://stackoverflow.com/questions/11147187/am-i-right-to-ignore-the-compiler-warning-for-lacking-await-for-this-async-call)

While I understand the fire and forget options, many of the cases in this example were due to the use of a dialog box, which should be awaited (otherwise it isn't really a blocking dialog and could disappear into the background). I believe I've seen a number of bugs in Win8 apps when run in Win 8.1 where the dialog box is not dismissed and remains over the screen indefinitely. It's just better to take the extra step and do this right where possible.

In addition, if you don't await the awaitable, if an exception is thrown in the awaitable, it will be lost. See http://msdn.microsoft.com/en-us/library/hh965065(v=vs.110).aspx in particular this: An exception that’s raised in a method that returns a Task or Task is stored in the returned task. If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown.