Home >Backend Development >C++ >What's the key difference between `await Task.Run(); return;` and `return Task.Run()` in C# async programming, and how does this impact exception handling?
The key differences between await Task.Run(); return;
and return Task.Run()
in the asynchronous programming and the impact on abnormal treatment
The main difference between the two code fragments is abnormal communication. In the "" version, the abnormalities thrown in the task will be captured and stored in the returned Task object. When waiting for the task, abnormalities will be spread. On the contrary, the "" version does not wait for the task, so no abnormality thrown will be captured, but immediately throw it on the call.
code generation await Task.Run(); return;
return Task.Run();
Abnormal treatment
Consider the following example: await Task.Run(); return;
return Task.Run();
" version will throw an exception when waiting for the task. However, if we call , the exception will be thrown immediately. (Suppose The function is properly handled abnormal).
asynchronous void method
<code class="language-csharp">static async Task OneTestAsync(int n) { await Task.Delay(n); } static Task AnotherTestAsync(int n) { return Task.Delay(n); }</code>For the asynchronous VOID method, the logic of abnormal communication is different. If it exists, the abnormalities thrown in the asynchronous VOID method will be thrown out in the context of the current thread. Otherwise, they will be thrown through , and the caller cannot handle them on the same stack frames.
DoTestAsync(OneTestAsync, -2)
Simulation asynchronous abnormal dissemination await
DoTestAsync(AnotherTestAsync, -2)
DoTestAsync
You can use a technique to simulate the abnormal communication behavior of asynchronous methods. It is used for non -specific ways based on TASK:
In this example, abnormalities will be thrown inside the task, and then spread through the object, similar to the situation of
.
ThreadPool.QueueUserWorkItem
The possibility of dead locks
Asynchronous/waiting version is easier to occur in non -default synchronization context. For example, the following code will occur in WinForms or WPF applications:
This is because trying to wait for the completion of the asynchronous operation on the UI thread, and the asynchronous operation itself also needs to be executed on the UI thread, resulting in dead locks.
In short, await Task.Run(); return;
provides structured abnormal treatment, and return Task.Run();
directly throw the abnormality to the call party, and the call party needs to be handled by themselves. The method to choose depends on your abnormal processing strategy and program context. await
The version is safer, but it is necessary to consider the potential dead lock problem; return Task.Run()
more concise, but the abnormal processing requires more carefulness.
The above is the detailed content of What's the key difference between `await Task.Run(); return;` and `return Task.Run()` in C# async programming, and how does this impact exception handling?. For more information, please follow other related articles on the PHP Chinese website!