locked
Async correct syntax RRS feed

  • Question

  • Hi

    I have below code at the end. GenerateFileList is a long running function and I want to run it asynchronously. However with below code I am getting

    This async method lacks 'Await' operators and so will run synchronously. Consider using the 'Await' operator to await non-blocking API calls, or 'Await Task.Run(...)' to do CPU-bound work on a background thread.

    on GenerateFileList.

    What am I missing and how can I correctly run GenerateFileList asynchronously?

    Thanks

    Regards

        Private Async Function ImportCSVFiles() As Task(Of Boolean)
    
            Dim x As Task(Of Boolean) = GenerateFileList("*.csv")
    
            Dim result = Await x
    
            If Not x.Result Then
               '...
            End If
    
            Return True
        End Function
    
        Public Async Function GenerateFileList(FileSpec As String) As Task(Of Boolean)
    
    
    
        End Function


    • Edited by Y a h y a Sunday, October 7, 2018 6:09 AM
    Sunday, October 7, 2018 6:08 AM

Answers

  • Here is the basic pattern,  the return statements are hard coded and would change depending on how code flows.

    Private Async Function ImportCSVFiles() As Task(Of Boolean)
        Dim result = Await GenerateFileList(".csv")
        Return True
    End Function
    
    Private Function GenerateFileList(FileSpec As String) As Task(Of Boolean)
        Return Task.Factory.StartNew(Of Boolean)(
            Function() As Boolean
                '
                ' do work
                '
                Return False
            End Function)
    End Function


    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 Y a h y a Sunday, October 7, 2018 10:14 AM
    Sunday, October 7, 2018 9:54 AM