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"