-1

In C#, does it make sense to write code like: await Console.In.ReadLineAsync()? because the application has to wait for user's response to decide what to do next anyway. so I might as well right something like:

Console.In.ReadLine();
qed59
  • 51
  • 7
  • Depends on what other things the application is doing. `await`-ing let's the current thread be used for other work in the meantime. See [Asynchronous programming with async and await](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/) – Xerillio May 29 '21 at 08:36
  • Hi Xerillio, in that example, there are 2 tasks been started. one is eggTask, and the other is baconTask. then code is in the order of awaiting for eggTask to complete, and write "eggs are ready", then it awaits for baconTask to finish and writes "bacon is ready". Let's say bacon task completes first. Does it write "bacon is ready" first? then "eggs are ready"? – qed59 May 29 '21 at 09:01
  • 2
    *Unfortunately* there is no difference, see https://stackoverflow.com/questions/14724582/why-does-console-in-readlineasync-block. – Klaus Gütter May 29 '21 at 10:30
  • @qed59 Not quite. The code still executes in the same order it's written there. But if you e.g. start multiple tasks **before** `await`-ing any of them, then it's up to the underlying task scheduler to control which one gets to complete first. – Xerillio May 29 '21 at 10:32
  • See first duplicate for why there is no difference at all between the two approaches. For the _theoretical_ answer to the question, ignoring the fact that the `Console`'s async support is broken, see the second duplicate. Use `await` if your code can take advantage of not tying up a thread; don't use it if it can't. A console app has the same overhead for a thread as any other program, so it's the _architecture_ of your program that determines whether `await` is useful, not the question of whether or not it's a console app. – Peter Duniho May 30 '21 at 16:08
  • Thank you everyone for your answer/comments. After reading them and the above asked questions I think I know the answer to my question now. I think there is a slight difference between them. I think the difference is, for Console.In.ReadLineAsync(), A state machine is generated and it return the thread back to the thread pool (with some overheads on performance) and it really isn’t blocking. But because this is a single-threaded console application, whether it’s blocking or not, it doesn’t really matter. Too bad I can’t award any points, I can only thank you for your helps. – qed59 Jun 03 '21 at 01:48

1 Answers1

0

It is similar if you want use them in a single task console application. Everything changes when you want to have a multitasking console application and have to run parallel tasks with ongoing one.

  • Console.ReadLineAsync(): Returns Task A task that represents the asynchronous read operation. The value of the TResult parameter contains the next line from the stream, or is null if all the characters have been read.
  • Console.ReadLine(): Returns String The next line of characters from the input stream, or null if no more lines are available.
    using System;
    using System.IO;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static async Task Main()
            {
                await ReadCharacters();
            }
    
            static async Task ReadCharacters()
            {
                String result;
                using (StreamReader reader = File.OpenText("existingfile.txt"))
                {
                    Console.WriteLine("Opened file.");
                    result = await reader.ReadLineAsync();
                    Console.WriteLine("First line contains: " + result);
                }
            }
        }
    }

By your request I Changed this code to bellow

class Program
    {
        static async Task Main()
        {
            Task thisTask= Task.Run(()=>ReadCharacters());
            int res = LogicalOperation(5, 6);
            await thisTask;
            
            Console.ReadLine();
        }

        static async Task ReadCharacters()
        {
            String result;
            using (StreamReader reader = File.OpenText("existingfile.txt"))
            {
                Console.WriteLine("Opened file.");
                result = await reader.ReadLineAsync();
                Console.WriteLine("First line contains: " + result);
            }
        }

        static int LogicalOperation(int x,int y )
        {
            Console.WriteLine("result of logial Operation is:" + (x*y).ToString());
            return x*y;
        }
    }

this image is only for better understanding enter image description here

Armineh
  • 86
  • 5
  • Hi Armineh, the above is just a single task application right? are you able to give me a multitask example? thanks – qed59 May 29 '21 at 09:11