Reputation: 50190
I am adding documentdb to an existing application (~500kloc). There is no way I can do an 'async all the way' rewrite of this app even if I wanted to.
All the C# APIs are async . The obvious way to sync with these calls is to .Wait()
them. This is what several MS sample apps do, however when I previously asked a similar question the consensus was 'do not do .wait you risk deadlocks'
I found this exact question in SO that had no answer, just comments. One said 'do .wait', one said 'use await' on said 'you cant do it'
'use await' doesnt resolve the question since we are still async, then none of these comments provide an answer
Upvotes: 2
Views: 754
Reputation: 816
Just calling YourMethodAsync().GetAwaiter().GetResult()
to run it synchronously and avoid deadlock
Upvotes: 0
Reputation: 327
Blocking calls (n samples are due to lack of 'async main' before C#7. Its recommended to not use blocking calls in you application as much as possible.
Upvotes: 1
Reputation: 2388
You could call your async method from a non-async method using:
var myThread = System.Threading.Tasks.Task.Run(async () => await
DoSomethingAsync());
And then you can avoid deadlock by calling
myThread.Wait();
Upvotes: 2
Reputation: 766
All DocumentDB async API will use Task.Run to run the task when there is a synchronization context (ASP/WPF/Winforms). That means, it is safe to call .Result or .Wait directly on DocumentDB C# API.
Upvotes: 4