Why is nothing printed on the console?

I create 2 methods that print x and y 100 times. I want them to run in parallel, and I expect the result to be xxxxyxyyyxyyxyx ... sthg. He doesn't print anything. Am I missing any logic here?

using System; using System.Threading.Tasks; namespace ConsoleApplication32 { internal class Program { public static async Task<int> Print1() { await Task.Run(() => { for (int i = 0; i < 100; i++) { Console.Write("x"); } }); return 1; } public static async Task<int> Print2() { await Task.Run(() => { for (int i = 0; i < 100; i++) { Console.Write("y"); } }); return 1; } public static void Run() { Task<int> i = Print1(); Task<int> k = Print2(); } private static void Main(string[] args) { Run(); } } } 
+5
source share
3 answers

The problem is that you do not wait for the completion of tasks, so the program terminates (no more than synchronous code blocking).

If you want tasks to run simultaneously, do this instead:

 public static void Run() { Task<int> i = Print1(); Task<int> k = Print2(); Task.WaitAll(i, k); } 

Another way to wait for the completion of these tasks would be to:

 public static async Task Run() { Task<int> i = Print1(); Task<int> k = Print2(); await Task.WhenAll(i, k); } public static void Main(string[] args) { Run().GetAwaiter().GetResult(); } 
+2
source

Explanation

There are several things that we need to configure to get the desired result: xxxxyxyxyyyxyyxyx .

  • The Run method does not wait for Task i and Task k

    • This means that the console application will close before the completion of two Task
    • As Andrei Matrasaru mentions in his answer, if you add Console.ReadLine() , this will prevent the console application from closing before the lines are printed on the screen.
    • A more elegant solution is to use Task.WhenAll . Task.WhenAll will allow the code to output its calling thread (in this case, the Main thread), which does not block the user interface of the application and allows the application to continue to print on the screen until Task finishes.This may seem trivial for a simple console application, but extremely It’s important to never block the main thread when creating any user interactive application, such as mobile or web applications; Locking the main theme is what causes applications to freeze.
    • Camilo Terevinto answer above recommends Task.WaitAll . This is also a bad practice, because it also freezes the application; Task.WaitAll not expected and cannot yield to the calling thread.
  • Print1() and Print2() never return to the main topic.

    • These methods execute in the background thread, but they do not include await inside the for loop
    • Inside for loop , await needed to allow the CPU to continue executing the main thread, because Console.Write() can only print on the main thread screen.

Code

 using System; using System.Threading.Tasks; namespace ConsoleApplication32 { internal class Program { public static async Task<int> Print1() { for (int i = 0; i < 100; i++) { Console.Write("x"); await Task.Delay(10); } return 1; } public static async Task<int> Print2() { for (int i = 0; i < 100; i++) { Console.Write("y"); await Task.Delay(10); } return 1; } public static async Task Run() { var i = Print1(); var k = Print2(); await Task.WhenAll(i, k); } private static void Main(string[] args) { Task.Run(async () => await Run()).GetAwaiter().GetResult(); } } } 

Output

xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy any

C # 7.1 Alternative

C # 7.1 introduces the ability to have an async Main method. This means that we can configure the Main method as follows:

  ... private static async Task Main(string[] args) { await Run(); } ... 
+11
source

Tried your code and it works fine. You probably think this is not working because the console window closes very quickly. Add Console.ReadLine() to your main:

 private static void Main(string[] args) { Run(); Console.ReadLine(); } 
+5
source

Source: https://habr.com/ru/post/1270782/


All Articles