Home >Backend Development >C++ >Await Task.Run(); return; vs. return Task.Run(): What's the Crucial Difference in Async Programming?
and in asynchronous programming await Task.Run(); return;
return Task.Run();
In asynchronous programming, understanding the difference between
is very important. Although these two structures look similar, their behavior is essentially different: await Task.Run();
return Task.Run();
Disposure:
A significant difference is that abnormal dissemination. The abnormalities thrown in the asynchronous method are stored in the
object returned, and the state is observed before the task is observed. Instead, in the non -exotic method, any throwing abnormality will immediately be triggered on the same thread, making it easier to capture.
Task
Synchronous context perception: Task
Task
The possibility of dead lock:
In some cases, calling Task
in the UI thread may lead to a deadlock. This is because the thread is blocked and waits for await
to complete, thereby effectively preventing UI from updating. Task
Avoid this problem by running tasks.
Code difference:
and generated code. The former generates an asynchronous state, while the latter creates a await Task.Run()
object. Asynchronous state machine allows methods to hang and recover after the task is completed. Task
return Task.Run()
In short, although and
The above is the detailed content of Await Task.Run(); return; vs. return Task.Run(): What's the Crucial Difference in Async Programming?. For more information, please follow other related articles on the PHP Chinese website!