How do you call an async function from Main on a console app?

Boruch Tkatch 141 Reputation points
2020-12-25T17:06:56.7+00:00

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?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,627 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113.8K Reputation points
    2020-12-25T17:47:50.993+00:00

    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

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Mauricio Moccelin 6 Reputation points
    2020-12-25T17:21:52.093+00:00

    @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.

    1 person found this answer helpful.

  2. Karen Payne MVP 35,286 Reputation points
    2020-12-26T04:02:24.8+00:00

    Hello @Boruch Tkatch

    VB.NET does not support async main, here is how to do this then look at the C# example.

    • VB.NET Source
    • C# Source
       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  
      

    C# version

    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;  
      
            }  
      
     }  
    }  
    
    1 person found this answer helpful.
    0 comments No comments