How do I call a Async sub or function from the Main? RRS feed

  • Question

  • User-583701291 posted


    How do I call a Async sub or function from the Main?

    Public Async Sub SearchAlerts(f As String)

    Dim returnedTaskTResult As Task = ProximityAlerts.RunAsync(f)

    Await returnedTaskTResult

    End Sub

    Sub Main()


    Thursday, August 15, 2019 5:39 PM

All replies

  • User753101303 posted


    https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/ is for C# but I guess they have done the same changes for VB ie try Async Sub Main() which should be supported now. Which VB version do you use ?

    Edit: I gave this a try and it doesn't seems to be supported (though I don't find where I can see and check which VB version I'm using). It seems you'll have to use :

    SearchAlerts("Sample").Wait() or

    Dim result=SearchAlerts("Sample").Result ' For now it doesn't return a result but it seems weird ???

    Thursday, August 15, 2019 6:17 PM
  • User-583701291 posted

    Thank you!!

    This is what I did and it works.

    Finally my problem was related to the .Net framework In 4.5.x the call  failed:  Dim response As HttpResponseMessage = Await client.GetAsync(URL)

    but in 4.6.1 it works fine.

    Public Sub SearchAlerts(f As String)

                    ' option 1
                    Dim result = SearchProximityAlertGetAsyncHttp("3").GetAwaiter().GetResult()

                    '' option 2
                    'Dim callTask As Task(Of GResults) = Task.Run(Function() SearchProximityAlertGetAsyncHttp("3"))
                    'Dim gData As GResults = callTask.Result
                Catch ex As Exception
                    Console.WriteLine("Exception: " & ex.Message)
                End Try

    End Sub

    Sub Main()

    End Sub

    Thursday, August 15, 2019 7:52 PM