none
how to return a class object from an async call RRS feed

  • Question

  • I have an Async function that is built for logging. It calls 
                result = cmd.BeginExecuteNonQuery()
                cmd.EndExecuteNonQuery(result)

    but I wish to implement passing an error handling class out to the caller.. so here is an abstract of the function

    Friend Async Function ExecuteNonQueryAsync(some parameters) as task(of MyResults (which is a class))

    try
            result = cmd.BeginExecuteNonQuery()
            cmd.EndExecuteNonQuery(result)

    catch

    Myresults.ERROR=A
    MyResults.STATUS=B
    end try

    Return(Myresults)

    end function

    The return(Myresults) may be incorrect - the error message I get is: the async method lacks "Await" operators and so will run synchronously.

    I do not understand how to implement it in my code above

    Thanks


    GADOI

    Friday, March 2, 2018 3:16 PM

Answers

  • You need to use await on a function that is marked with Async

    e.g. use await on ExecuteNonQueryAsync, don't use ExcuteNonQuery.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by GAINSCOM Friday, March 2, 2018 4:44 PM
    Friday, March 2, 2018 3:31 PM
    Moderator
  • Thank you Reed - great progress... I see you are returning localResults.
    though when I am calling this function from another function I am getting the error that 

    ...MyNewresults as results =  ExecuteNonQueryAsync(some, parameters)...
    that
    MyNewresults .. value of type task(of Results) cannot be converted to MyNewresults.

    So my objective is to get results out of 
    ExecuteNonQueryAsync(some, parameters) so that I can analyse them.

    Thanks


    GADOI

    You still are forgetting to include the Await keyword when you call the Async method:

    Dim localResult As Results = Await ExecuteNonQueryAsync(some, parameters)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by GAINSCOM Friday, March 2, 2018 6:55 PM
    Friday, March 2, 2018 6:53 PM
    Moderator

All replies

  • You need to use await on a function that is marked with Async

    e.g. use await on ExecuteNonQueryAsync, don't use ExcuteNonQuery.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by GAINSCOM Friday, March 2, 2018 4:44 PM
    Friday, March 2, 2018 3:31 PM
    Moderator
  • Thank you Karen - my next question (perhaps a new thread :-)) .. but just in case....

    So, my objective is to return MyResults  (not a task status), so I am not clear on this.
    If I have in a different function an ORIGINAL  call like:

    Dim MyResults as results = ExecuteNonQueryAsync(some parameters)

    now it appears I have to call it this way...
    Dim MyTask As Task(Of results)
    MyTask = ExecuteNonQueryAsync(some parameters)

    my question is how do I get MyResults class payload out of Friend Async Function ExecuteNonQueryAsync(some parameters) as task(of MyResults)

    Thank you for your time!

    T


    GADOI

    Friday, March 2, 2018 4:49 PM
  • You can do it that way and then await MyTask:

    Dim MyTask As Task(Of Results)
    MyTask = ExecuteNonQueryAsync(some, parameters)
    Dim localResult As Results = Await MyTask
    

    But if you have no need to analyze or manipulate the task instance then you do not need to explicitly reference it.  You can just do:

    Dim localResult As Results = Await ExecuteNonQueryAsync(some, parameters)
    One reason to create the task variable first would be to execute multiple concurrent tasks and await a collection of task instances.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 2, 2018 6:10 PM
    Moderator
  • Also, your async function isn't implemented correctly.  You need to use TAP over EAP to wrap the EAP async calls into an awaitable task; that is what will make your method async.  Here's an example that should be more along the lines of what you are trying to do:

    Friend Async Function ExecuteNonQueryAsync(some, parameters) As Task(Of Results)
        Dim localResults As New Results
        Try
            Dim commandResult As Integer
            Dim queryTask = Task.Factory.FromAsync(cmd.BeginExecuteNonQuery(), Sub(iar As IAsyncResult)
                                                                                   commandResult = cmd.EndExecuteNonQuery(iar)
                                                                               End Sub)
            Await queryTask
            localResults.Value = commandResult
        Catch e As Exception
            localResults.ErrorMessage = e.Message
        End Try
        Return (localResults)
    End Function
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 2, 2018 6:19 PM
    Moderator
  • Might be good to explain EAP and TAP :-)

    I'm in ColdFusion today, no Visual Studio for several hours so I can't write a sample, Reed's should do the trick.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 2, 2018 6:33 PM
    Moderator
  • Thank you Reed - great progress... I see you are returning localResults.
    though when I am calling this function from another function I am getting the error that 

    ...MyNewresults as results =  ExecuteNonQueryAsync(some, parameters)...
    that
    MyNewresults .. value of type task(of Results) cannot be converted to MyNewresults.

    So my objective is to get results out of 
    ExecuteNonQueryAsync(some, parameters) so that I can analyse them.

    Thanks


    GADOI

    Friday, March 2, 2018 6:45 PM
  • Thank you Reed - great progress... I see you are returning localResults.
    though when I am calling this function from another function I am getting the error that 

    ...MyNewresults as results =  ExecuteNonQueryAsync(some, parameters)...
    that
    MyNewresults .. value of type task(of Results) cannot be converted to MyNewresults.

    So my objective is to get results out of 
    ExecuteNonQueryAsync(some, parameters) so that I can analyse them.

    Thanks


    GADOI

    You still are forgetting to include the Await keyword when you call the Async method:

    Dim localResult As Results = Await ExecuteNonQueryAsync(some, parameters)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by GAINSCOM Friday, March 2, 2018 6:55 PM
    Friday, March 2, 2018 6:53 PM
    Moderator
  • Note that this means that the calling method must also be an Async method.  What does the method which is calling ExecuteNonQueryAsync look like?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 2, 2018 6:55 PM
    Moderator