I really love using async, I tried with async/await operators and it works like a charm.
What happens though when you just return a Task<T>
and Wait()
to finish because you want to get the result synchronously.
Asynchronous is nice but I will have a couple of situations where I want to use the same methods synchronous.
For example, this will never get to the Console.WriteLine()
private void PrintFileToConsole()
{
FileRepositoy fileRepo = new FileRepositoy ();
string contents= fileRepo.Read(Path.Combine("Directory", "file.dat")).Result;
Console.WriteLine ("File contents: {0}", contents== string.Empty ? "No contents" : contents);
}
But this will work just fine
private async void PrintFileToConsole()
{
FileRepositoy fileRepo = new FileRepositoy ();
string contents= await fileRepo.Read (Path.Combine ("Directory", "file.dat"));
Console.WriteLine ("Last Exception: {0}", contents== string.Empty ? "No contents" : contents);
}
Is this a known issue?