1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskConsole
{
    class Program
    {
        static void Main(string[] args)
        {       
             test();    
        }

        static async Task<string> ReadTextAsync() 
        {
            string textContents;
            Task<string> readFromText;

            using (StreamReader reader =  File.OpenText("email.txt"))
            {
                readFromText = reader.ReadToEndAsync();
                textContents = await readFromText;

            }

            return textContents;     
        }

        static async Task test ()
        {
            string capture = await ReadTextAsync();
            Console.WriteLine(capture);
        }               
    }               
}

I have the following code to read from a text file using async. I learned from this post that the example that Microsoft implemented using StreamReader is incorrect, so as a learning exercise, I decided to correct it. How would I properly call the test method from main, when the test method doesn't return any task. I did a little reading and learned it's bad practice to use async void. In my case what should I do?

Side Note: I don't know if I implemented it wrong, but I can't get my text to display. I tried it the non-async way and it worked, however, when I use async it shows blank, and Please Press Any Key to Continue"

Community
  • 1
  • 1
  • Why would you want to do this asynchronously as you are waiting for it anyway and making it synchronous? A lot of effort to achieve default synchronous flow? – Belogix Jun 29 '15 at 14:35
  • @sstan - It Work! Thanks. –  Jun 29 '15 at 14:35
  • @Belogix - No particular reason, as I said in the above post, Microsoft implemented it wrong, so as a learning exercise, I decided to correct it. –  Jun 29 '15 at 14:36
  • 1
    possible duplicate of [async at console app in C#?](http://stackoverflow.com/questions/17630506/async-at-console-app-in-c) – Sinatr Jun 29 '15 at 14:40
  • You do not need to have the await in both places. http://stackoverflow.com/questions/13167934/how-to-async-files-readalllines-and-await-for-results – Shawn M Holman Jun 29 '15 at 14:40
  • @Belogix - What else should I do it make it asynchronous? –  Jun 29 '15 at 14:41
  • @ShawnMHolman - How would you correct it, so that await isn't in both places? –  Jun 29 '15 at 14:44

2 Answers2

5

How would I properly call the test method from main, when the test method doesn't return any task.

Because Main can't be modified to be async, you'll have to explictly call Task.Wait on it:

Test().Wait();

This is the only place you should be blocking on an async call.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
0
static async Task<string> ReadTextAsync() 
        {
            string textContents;
            Task<string> readFromText;

            using (StreamReader reader =  File.OpenText("email.txt"))
            {
                readFromText = await Task.Run(() => reader != null ? reader.ReadToEndAsync() : null);
                textContents = readFromText;

            }

            return textContents;     
        }

        static Task test ()
        {
            string capture = ReadTextAsync();
            Console.WriteLine(capture);
        }               
  • Just out of curiosity, the way I was doing it previously, is it the incorrect way? What is the downside of doing it the way I was doing it as opposed to the way you've shown? –  Jun 29 '15 at 14:55
  • http://stackoverflow.com/questions/18013523/when-correctly-use-task-run-and-when-just-async-await – Shawn M Holman Jun 29 '15 at 15:05