Synchronously waiting on Task
, ValueTask
, or awaiters is dangerous and may cause dead locks.
void DoSomething()
{
DoSomethingElseAsync().Wait();
DoSomethingElseAsync().GetAwaiter().GetResult();
var result = CalculateSomethingAsync().Result;
}
Please consider the following options:
- Switch to asynchronous wait if the caller is already a "async" method.
- Change the chain of callers to be "async" methods, and then change this code to be asynchronous await.
- Use
JoinableTaskFactory.Run()
to wait on the tasks or awaiters.
async Task DoSomethingAsync()
{
await DoSomethingElseAsync();
await DoSomethingElseAsync();
var result = await CalculateSomethingAsync();
}
void DoSomething()
{
joinableTaskFactory.Run(async delegate
{
await DoSomethingElseAsync();
await DoSomethingElseAsync();
var result = await CalculateSomethingAsync();
});
}
Refer to Asynchronous and multithreaded programming within VS using the JoinableTaskFactory for more information.