Home

Beta: Util.CompileAsync - await operator

Hi,

I am trying to use the new CompileAsync method (from within LinqPad v4.53.16), but the following code

void Main() { var q = await Util.CompileAsync("ScriptPath.linq"); }

throws this exception:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

Any ideas why this is not working?

Comments

  • edited February 2015
    You need to do exactly as the compiler suggests. Mark your main method as async:
    async void Main()
    {
       var q = await Util.CompileAsync("ScriptPath.linq");
    }
    or better:
    async Task Main()
    {
       var q = await Util.CompileAsync("ScriptPath.linq");
    }
    Both do the same thing, but the latter is nicer because LINQPad will show the query as "running" until the asynchronous method logically completes.

    In a normal Visual Studio project, the main method cannot be marked as async. But because LINQPad is a scratchpad (a hosted environment), the rules have been relaxed to improve the experience.
  • Thanks,

    I would have never thought of making the main method async :-)
Sign In or Register to comment.