0

It's giving me build error (i am using vs 2017), but in error list there is no error found

public static async void Main(string[] args)
{                     
    await LongOP1();            
}

public static async Task LongOP1()
    {
        long x = 0;

        await Task.Run(() =>
        {
            for (int i = 0; i <= 10000; i++)
            {
                for (int j = 0; j <= 10000; j++)
                {
                    x += i + j;
                }
            }
        });         
    }
shak imran
  • 332
  • 1
  • 4
  • 13

2 Answers2

2

You cannot use the async keyword on the Main method yet.

See this for an alternative, and look at other answers in the thread for an explanation : https://stackoverflow.com/a/24601591/4587181

Relevant code :

static void Main(string[] args)
{
    Task.Run(async () =>
    {
        // Do any async anything you need here without worry
    }).GetAwaiter().GetResult();
}
FatalJamòn
  • 386
  • 2
  • 10
0

I prefer to do it this way

public static void Main()
{
    Task t = LongOP1();

    //  Do other stuff here...

    t.Wait();
}

public static async Task LongOP1()
{
    long x = 0;

    await Task.Run(() =>
                   {
                       for (int i = 0; i <= 10000; i++)
                       {
                           for (int j = 0; j <= 10000; j++)
                           {
                               x += i + j;
                           }
                       }
                   });         
}   
William Han
  • 78
  • 1
  • 7
  • Usually I'd do it this way but turns out the getAwaiter doesn't wrap exceptions in a AggregateException. At least from what people are saying in the thread I pointed to. – FatalJamòn Jul 13 '17 at 06:20