Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net 4.5 async syntax

I wrote the code below,

Task.Factory.StartNew<int>(async () =>
        {
            await Task.Delay(1000);
            return 42;
        });

but the read line appeared under the "async" keyword, and the code cannot compiled due to some syntax error, can anybody advise me what to do?

Thx a lot!

like image 278
iNc0ming Avatar asked Jun 20 '26 11:06

iNc0ming


1 Answers

You probably want to use Task.Run, which has more natural syntax for async lambdas:

var task = Task.Run(async () =>
{
  await Task.Delay(1000);
  return 42;
});
like image 192
Stephen Cleary Avatar answered Jun 23 '26 03:06

Stephen Cleary