Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

41
  • 23
    Solution A seems like what I want, but it looks like task.WaitAndUnwrapException() didn't make it into the .Net 4.5 RC; it only has task.Wait(). Any idea how to do this with the new version? Or is this a custom extension method you wrote? Commented Jul 25, 2012 at 23:17
  • 6
    WaitAndUnwrapException is my own method from my AsyncEx library. The official .NET libs don't provide much help for mixing sync and async code (and in general, you shouldn't do it!). I'm waiting for .NET 4.5 RTW and a new non-XP laptop before updating AsyncEx to run on 4.5 (I cannot currently develop for 4.5 because I'm stuck on XP for a few more weeks). Commented Jul 25, 2012 at 23:33
  • 21
    AsyncContext now has a Run method that takes a lambda expression, so you should use var result = AsyncContext.Run(() => MyAsyncMethod()); Commented Jun 23, 2013 at 12:42
  • 5
    @Asad: Yes, more than 2 years later the API has changed. You can now simply say var result = AsyncContext.Run(MyAsyncMethod); Commented Apr 14, 2014 at 23:55
  • 12
    @bluejayke: Install the Nito.AsyncEx library. Alternatively, use .GetAwaiter().GetResult() instead of .WaitAndUnwrapException(). Commented Feb 6, 2020 at 14:03