Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Wait() or Result in async/await methods are never actually return and make the app to hang forever

$
0
0

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?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>