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

  • Question

  • User-583701291 posted

    Hello,

    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

    Hi,

    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)
                Try

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

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

    End Sub

    Sub Main()
     

                SearchAlerts("3")
           
    End Sub

    Thursday, August 15, 2019 7:52 PM