Home > Backend Development > C++ > Async/Await: Should I Return a Task or Void?

Async/Await: Should I Return a Task or Void?

Susan Sarandon
Release: 2025-02-01 14:06:10
Original
418 people have browsed it

Async/Await: Should I Return a Task or Void?

The return value of the async/await method: TASK or VOID?

In asynchronous programming, the return value of the method is

or

will have a significant effect. This article will explore the applicable scenes of each choice. async Task Return to task void

Usually, the method should return . This allows calling code this task and tracking its progress when necessary. The main exception of this rule is when you clearly need

to return the type, such as handling events.

async Return to VOID Task await void Use the return type

method to mark it as "top asynchronous operation". The behavior of these operations is different when abnormal. Different from the of the return value, the

abnormality in the asynchronous method will not be observed under default. They will become an unprepared exception and may trigger the processing program.

Consider the following example:

void async In this example, Task and void Keywords are unnecessary because there is no need to explicitly handle abnormalities. However, if abnormalities occur, it will not be observed and may not be processed correctly. TaskScheduler.UnobservedTaskException

Example of abnormal treatment

public static async void AsyncMethod2(int num)
{
    await Task.Factory.StartNew(() => Thread.Sleep(num));
}
Copy after login
In order to demonstrate the differences between abnormal treatment between <和> and

asynchronous methods, please consider the following code: async await

If you call <,>, you can observe the abnormality and see it as any other unprocessed abnormalities. However, if <则> is called, it will never observe the abnormalities and will be processed by the

processing program, which will lead to unfarished behaviors. <> Best practice

Task As the best practice, unless you clearly need void to return the type, you will always return

from the
static async void f()
{
    await h();
}

static async Task g()
{
    await h();
}

static async Task h()
{
    throw new NotImplementedException();
}
Copy after login
method. This ensures that the abnormalities are properly handled, and the caller can choose

the task and track its progress. f g For more details, please refer to Microsoft's document on TaskScheduler.UnobservedTaskException/

Best Practice:

https://www.php.cn/link/d3ddedd999aa150F47C2D0A55CB17898

The above is the detailed content of Async/Await: Should I Return a Task or Void?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template