locked
Using Async / await with do while loop and class

    Question

  • Hello I have the following code which I wish to run asynchronously but can't seem to see how.

       Private Async Function SendTurntoServer() As Task
              Do While ClassMySqlUpdateToDatabase.updatedb() = False
            Loop
        End Function

    This function simply writes to the mysql server and then results back true or false for weather it succeeded in writing to the server. If it fails it tries again until it succeeds. But I don't want this to lag the main UI. I made the calling function Async and tried to await the do while loop but it vs says that it's not awaitable. so how do I run it async then. I can't find any goo info about  What is awaitable or not anywhere. 

    Sunday, May 04, 2014 12:39 AM

Answers

  • I would make ClassMySqlUpdateToDatabase.updated() return a task of boolean

    I would change the loop to something like this

       Private Async Function SendTurntoServer() As Task
    Dim IsFinished as Boolean = false
               Do While IsFinished = false
    IsFinished= await ClassMySqlUpdateToDatabase.updatedb()
             Loop
         End Function

    • Marked as answer by PBPuddin Tuesday, May 06, 2014 3:44 PM
    Sunday, May 04, 2014 11:00 AM
  • You can only await methods that has one of the three following return types:  Task<TResult>, Task, and void. In Visual Basic, the void return type is written as a Sub procedure.

    This means that your method must return a Task(Of Boolean) instead of a Boolean in order to be awaitable. Please refer to the following page on MSDN for more information about async return types : http://msdn.microsoft.com/en-us/library/hh524395.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    • Marked as answer by PBPuddin Tuesday, May 06, 2014 3:44 PM
    Sunday, May 04, 2014 11:59 AM

All replies

  • I would make ClassMySqlUpdateToDatabase.updated() return a task of boolean

    I would change the loop to something like this

       Private Async Function SendTurntoServer() As Task
    Dim IsFinished as Boolean = false
               Do While IsFinished = false
    IsFinished= await ClassMySqlUpdateToDatabase.updatedb()
             Loop
         End Function

    • Marked as answer by PBPuddin Tuesday, May 06, 2014 3:44 PM
    Sunday, May 04, 2014 11:00 AM
  • You can only await methods that has one of the three following return types:  Task<TResult>, Task, and void. In Visual Basic, the void return type is written as a Sub procedure.

    This means that your method must return a Task(Of Boolean) instead of a Boolean in order to be awaitable. Please refer to the following page on MSDN for more information about async return types : http://msdn.microsoft.com/en-us/library/hh524395.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    • Marked as answer by PBPuddin Tuesday, May 06, 2014 3:44 PM
    Sunday, May 04, 2014 11:59 AM