2

I have just read about special cases in console projects. Could you tell me whether my approach is right. Two jobs there i am not sure whether i should just use await inside Task.Run as i did here, if it is correct can you explain what would be the diffrence if i would delete both awaits from here. Next question what if i would remove .Wait() from WhenAny. Generally is it correct approach in console apps? Forget about proposing async Main so far, let's talk with void.

public class Program
{
  public static void Main()
  {
     //jobs can work in parael not blocking program
     var job0 = Task.Run(async () => await new DoThisAsync().Run());
     var job1 = Task.Run(async () => await new DoThatAsync().Run());

     //Any Independent synchronous work can run meantime jobs
     IndependentSynchronousMethod;

     //We wait on tasks
     Task.WhenAll(job0, job1).Wait();
  }

}
Arie
  • 109
  • 1
  • 7
  • 1
    `what would be the diffrence if i would delete both awaits from here.` What happened when you tried it? – mjwills Oct 30 '18 at 06:36
  • Assuming you've seen https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app... Can you just use latest VS and use async Main ? – Alexei Levenkov Oct 30 '18 at 06:39
  • Why don't you want to use async main? – mjwills Oct 30 '18 at 06:45
  • Working with net framework less than 4.5 and would liek to udnerstand behaviour – Arie Oct 30 '18 at 06:46

1 Answers1

8

Most of this code was not needed

Also if you make your Main async you can do the following

public static async Task Main()
{
    var job0 = DoThisAsync();
    var job1 = DoThatAsync();

    //Any Independent synchronous work can run meantime jobs
    IndependentSynchronousMethod;

    //We wait on tasks
    await Task.WhenAll(job0, job1)
}

To make your Main async

Project -> Build -> Advanced - Language Version >= 7.1

Additional Resources

Task.WhenAll Method

Creates a task that will complete when all of the supplied tasks have completed.

Can't specify the 'async' modifier on the 'Main' method of a console app

Update

Arie : to make this clear forget about async Main

With no async signature you could do this in a console app.

Task.WhenAll(job0, job1).Wait();

Your code was just wrong and wouldn't compile. However besides that, you were trying to wrap async calls in a task, which is redundant in this case. You are trying to create a task to run a task and its just not needed.

The async signature returns a task by default, Just pass them to WhenAll

If i just call Task.WhenAll(job0, job1) without Wait at the end program would end before waiting?

Calling an async method returns a task that has already been started. So there is no actual code necessary to force it to run. However, because the task is started something needs to wait for it some how, or in the example the process will end

Task.WhenAll aggregates the tasks, it doesn't start them for you, however if you don't wait for it, or await it, it its self just lets control flow through to the exit of the application

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • If i just call Task.WhenAll(job0, job1) without .Waitat the end program would end before waiting? – Arie Oct 30 '18 at 06:48
  • Yes, the program would end prematurely. – ProgrammingLlama Oct 30 '18 at 06:52
  • 1
    @Arie I cant explain everything to you in comments unfortunately, this will turn into a never ending cycle of questions probing every possible permutation of `Task` `await` `async`. I have explained why your code is redundant, if you have another further specific question please feel free to ask again – TheGeneral Oct 30 '18 at 07:11
  • @TheGeneral I understand it like because i have Wait on Task.WhenAll(job0, job1) then additional async () => await in my jobs calls are reduntant, correct? However if i wouldn't have Task.WhenAll(job0, job1).Wait(); then that would be fine am i right? – Arie Oct 30 '18 at 07:24
  • 1
    @Arie yes, and that is your comment question limit used up for today – TheGeneral Oct 30 '18 at 07:25
  • @TheGeneral so i can also say that my Task.WhenAll(job0, job1).Wait(); is redundant that's because at the moment job1 wont; be executed until job0 is done because of await inside Task.Run. please of last asnwer. – Arie Oct 30 '18 at 07:28