If the function does not return a value, then try Wait:
Task.Delay(4000).Wait()
If it returns a result, then try this:
Dim r = SomeAsyncFunction().Result
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a VB Winforms application that uses Await in Form1_Load and works fine. I would now like to switch this to being a console application. How do i call asynchronous subs or functions from Main?
If the function does not return a value, then try Wait:
Task.Delay(4000).Wait()
If it returns a result, then try this:
Dim r = SomeAsyncFunction().Result
@Boruch Tkatch I suggest Download .NET 5.0, possible entry points:
public static async void Main()
{
... // await code
}
See this link. I hope I helped you.
Hello @Boruch Tkatch
VB.NET does not support async main, here is how to do this then look at the C# example.
Imports System.IO
Module Module1
Sub Main()
MainAsync(New String() {"Hello", "Karen"}).
GetAwaiter().
GetResult()
Dim results = Task.WhenAll(
ExampleOne,
ExampleTwo).
Result
For Each result In results
Console.WriteLine(result)
Next
Console.ReadLine()
End Sub
Public Async Function MainAsync(ByVal args() As String) As Task
Await Task.Delay(3000)
Console.WriteLine($"{args(0)} {args(1)}")
End Function
Async Function ExampleOne() As Task(Of Integer)
Await Task.Delay(2000)
Return 40
End Function
Async Function ExampleTwo() As Task(Of Integer)
Await Task.Delay(5000)
Return 60
End Function
End Module
using System;
using System.Threading.Tasks;
namespace ConsoleAppAsync_csharp
{
class Program
{
static async Task Main(string[] args)
{
await Task.Delay(3000);
Console.WriteLine($"{args[0]} {args[1]}");
var results = Task.WhenAll(ExampleOne(), ExampleTwo()).Result;
foreach (var result in results)
{
Console.WriteLine(result);
}
Console.ReadKey();
}
public static async Task<int> ExampleOne()
{
await Task.Delay(2000);
return 40;
}
public static async Task<int> ExampleTwo()
{
await Task.Delay(5000);
return 60;
}
}
}