What is the reason for using the CS1998 method "not enough wait statements"

The C # compiler generates warning CS1998 when the method asynchas no statements await.

What are the reasons for the warning?

I know that it asyncinjects overhead into a method, adding statemachine and exception handling.

What is the main reason for the effectiveness of a warning? Or a reason to notify me that I might have forgotten somewhere await?

Perhaps someone from the language development team can shed light on this ... :)

(Please do not post answers that say, “You can remove the asyncwarning so that it disappears.” I want to know the reasons and solutions that underlie the warning, not the ways around it.)

+4
source share
1 answer

What is the reason for the warning?

Simply put, a method asyncthat does not use awaitis almost certainly erroneous. Not always wrong, or that would be a mistake. But almost always wrong, so a warning.

An incredibly common mistake of an asynchronous novice is to assume what it asyncmeans to "make this method asynchronous." This is usually associated with the assumption that “asynchronous” means “running in the background thread,” but sometimes it’s just an assumption of “magic.”

Thus, the warning clearly indicates that the code will work synchronously.

, - async, , .

, async await , (.., , ) , . async, TaskCompletionSource<T> try/catch. ; , .

+5

All Articles