2

I have a function that isn't async and as a result I can't use await. In that function I call a function that's async.

My coworker suggested I use:

private void Foo()
{
   var _ = BarAsync();
}

This way however has the disadvantage that I don't get notified when the function throws errors. Is there a better way to call the function? I don't care whether or not the function gets executed asyncronly.

3
  • You can synchronously block on it using Wait -> BarAsync().Wait(); Commented Nov 29, 2018 at 9:49
  • I'm trying to get the MS docs updated with this information. Go vote on this: github.com/dotnet/docs/issues/7995 Commented Nov 29, 2018 at 9:58
  • @AlexanderDerck : Does that allow me to receive Expection that get throw within the function? Commented Nov 29, 2018 at 10:25

1 Answer 1

0
var result = BarAsync()?.Result;

Or

var runningTask = BarAsync();
runningTask.Wait();
var result = runningTask?.Result;
Sign up to request clarification or add additional context in comments.

2 Comments

this is a very bad idea in the general case - and can even cause hard deadlocks; the only time you should ever access .Result is if you have checked and you know that it has completed
@Liam both can :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.