none
How do I call this function RRS feed

  • Question

  • Hi Experts, 

    Am very new to C#. I found some suitable piece of code that I wish to be executed on a console window but unable to get how can I call this function from the Main method. 

    async Task DoSomethingAsync()
    {
      int value = 13;
    
      
      await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    
      value *= 2;
    
      
      await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    
      Trace.WriteLine(value);
    }
    
    
    
    

    Thanks

    Priya

    Wednesday, November 6, 2019 4:28 PM

Answers

  • Hello,

    When targeting 4.7.1 or higher

    using System;
    using System.Diagnostics;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                await DoSomethingAsync();
            }
            static async Task DoSomethingAsync()
            {
                int value = 13;
    
    
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    
                value *= 2;
    
    
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    
                Trace.WriteLine(value);
            }
    
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 6, 2019 4:56 PM
    Moderator
  • You have to target C# 7.1 or greater to use async Main. Since you are new to C# and using Task.Delay, its important to know what is going on. Task.Delay is a timer based wait mechanism. Specifically, it will create a task which will complete after a time delay. Task.Delay is not a blocking call. The primary real-world use is as retry timers for I/O operations.

    Also, C# 8 is now available.

            static async Task Main(string[] args)
            {
                await DoSomethingAsync();
            }
    
            private static async Task DoSomethingAsync()
            {
                int value = 13;
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
                value *= 2;
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
                Trace.WriteLine(value);
            }



    william xifaras

    Thursday, November 7, 2019 1:21 AM
  • Hi Priya Bange, 

    Thank you for posting here.

    As William Xifaras suggested, C# 7.1 enables you to use async main to use await in your main method.

            static async Task Main(string[] args)
            {
                await DoSomethingAsync();
            }
    

    You can refer to the following reference to choose your C# language version.

    Choosing the C# Language latest version (minor release ) in Visual Studio 2017

    Besides, you can update your visual studio to visual studio 2019 whose default C# version is 7.3 and higher.

    C# language versioning

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 7, 2019 6:38 AM
    Moderator

All replies

  • Hello,

    When targeting 4.7.1 or higher

    using System;
    using System.Diagnostics;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                await DoSomethingAsync();
            }
            static async Task DoSomethingAsync()
            {
                int value = 13;
    
    
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    
                value *= 2;
    
    
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    
                Trace.WriteLine(value);
            }
    
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 6, 2019 4:56 PM
    Moderator
  • You have to target C# 7.1 or greater to use async Main. Since you are new to C# and using Task.Delay, its important to know what is going on. Task.Delay is a timer based wait mechanism. Specifically, it will create a task which will complete after a time delay. Task.Delay is not a blocking call. The primary real-world use is as retry timers for I/O operations.

    Also, C# 8 is now available.

            static async Task Main(string[] args)
            {
                await DoSomethingAsync();
            }
    
            private static async Task DoSomethingAsync()
            {
                int value = 13;
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
                value *= 2;
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
                Trace.WriteLine(value);
            }



    william xifaras

    Thursday, November 7, 2019 1:21 AM
  • Hi Priya Bange, 

    Thank you for posting here.

    As William Xifaras suggested, C# 7.1 enables you to use async main to use await in your main method.

            static async Task Main(string[] args)
            {
                await DoSomethingAsync();
            }
    

    You can refer to the following reference to choose your C# language version.

    Choosing the C# Language latest version (minor release ) in Visual Studio 2017

    Besides, you can update your visual studio to visual studio 2019 whose default C# version is 7.3 and higher.

    C# language versioning

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 7, 2019 6:38 AM
    Moderator