locked
Async Function or Async Sub?

    Question

  • I have an Async method that I am using as the handler for the Click event of several Buttons in my Windows Store app. I also call this method directly in several places. However, I am not sure whether the method should be a Sub or a Function. When I call the method directly, I use the following statement:

    Await Me.EditScore_Click(tempbutton, Nothing)


    And the signatures I have used for the method to try creating it as a Sub and Function are:

    Private Async Sub EditScore_Click(sender As Object, e As RoutedEventArgs)
    
    Private Async Function EditScore_Click(sender As Object, e As RoutedEventArgs) As Task

    When the method is a Sub, I get the following error:

    'EditScore_Click' does not return a Task and cannot be awaited. Consider changing it to an Async Function.

    When the method is a Function, I get the following warning for all the AddHandler statements in MainPage.g.vb that add the method to my Buttons in MainPage.xaml:

    The Task returned from this Async Function will be dropped, and any exceptions in it ignored. Consider changing it to an Async Sub so its exceptions are propagated.

    Basically, they both tell me to change it to the other one. I don't need the returned Task, so which one should it be, and how can I stop Visual Studio 2013 from giving the error or warning for whichever one I make it? Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, February 10, 2014 3:09 AM

Answers

  • Yes, that should be fine.

    In your new code you're Awaiting the Dispatcher's delegate, not the Click event. The Sub in the RunAsync call creates a new routine which calls your Click event.

    --Rob

    Monday, February 10, 2014 8:57 PM
    Owner

All replies

  • The problem is that you cannot Await a Click handler. You cannot await a sub, only a function which returns a Task. The Click handler is a RoutedEventHandler which returns void (aka: a Sub), so it cannot return a Task and be Awaited.

    If you need to await the actions from the Click handler elsewhere you can factor them out of the Click handler itself into a separate function and then Await that both from the other location and from inside EditScore_Click

    --Rob


    Monday, February 10, 2014 3:17 AM
    Owner
  • Then I'm not sure why my previous release worked when using a Function (although that release was for Windows 8.0, not Windows 8.1), but when I changed the handler to a Sub and the calling statement to:
    Await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, Sub() Me.EditScore_Click(tempbutton, Nothing))
    It seemed to work with no errors, so I guess that is OK? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, February 10, 2014 5:22 PM
  • Yes, that should be fine.

    In your new code you're Awaiting the Dispatcher's delegate, not the Click event. The Sub in the RunAsync call creates a new routine which calls your Click event.

    --Rob

    Monday, February 10, 2014 8:57 PM
    Owner