1

In C#, when we call an async method, it is needed to await it within the method, or the compiler will give out a warning message saying the method will be called sync without "await". I know this is useful in scenarios like:

var task = DoSomethingLongAsync(); DoSomethingElse(); await task; 

That is because when we call that async method, we can do something else, then wait for the async result.

But in some other cases, we don't need to wait the async result, like the scenario similar to web server:

listen(); while (true) { var request = Accept(); await ProcessRequestAsync(request); } 

Obviously, for above scenario, we hope the requests can be processed in parallel. But if we use await there (as current), the requests will be processed one by one, not as we expected.

So how should we call async methods in similar scenarios?

2

5 Answers 5

3

You don't have to use await - it's a warning, not an error.
The compiler is giving you this warning because the usual use case for async methods is to run them asynchronously, and to do that, you use the await keyword.

Basically, this warning is the compiler's way of giving you a heads up making sure that this doesn't go unnoticed - but it will not stop you from using async methods without the await keyword - your code will compile and run.

Sign up to request clarification or add additional context in comments.

Comments

1

As @Zohar Peled wrote, it's just a warning.

Write the following to make your intention clear to others.

In other words, just trash the resulting task because it's not relevant for you in that case.

listen(); while (true) { var request = Accept(); _ = ProcessRequestAsync(request); } 

Comments

1

if you can look this document you can see that :

The await operator doesn't block the thread that evaluates the async method.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await

So if you dont want to block your thread. You should use await. So that is way we are design our methods async!

If you design your method as async and call it without await. It means you deserved a warning :)

Comments

0

Most of the time, it's perfectly fine to not await an async method call.

You could just take the resulting task, and keep it for later

var requestTask = ProcessRequestAsync(request); 

or get a group of tasks and then wait for them all to complete, they will work in parallel

var task1 = ProcessRequestAsync(request1); var task2 = ProcessRequestAsync(request2); var task3 = ProcessRequestAsync(request3); await Task.WhenAll(new [] { task1, task2, task3 }); 

or just the first one, and do something with it while the others still work.

await Task.WhenAny(new [] { task1, task2, task3 }); 

You don't have to await any of them, as long as your application is still running, the tasks will run and complete.

6 Comments

But if the ProcessRequestAsync method does not have the await keyword anywhere, it will just run synchronously. What's the point of using the async keyword without await? Just remove it.
@ChrisDunaway that's true, but it's also not what I'm saying. OP is specifically asking about processing requests in parallel, which is what I'm demonstrating. In the situation where ProcessRequestAsync doesn't actually run any async code, then it will run synchronously, regardless of whether you call it using the async keyword or not. It will always block until completion, and the result will always be a completed task.
But is it truly parallel? The second call to ProcessRequestAsync will not start until the first is completed, and the third call will not start until the second is completed, unless I'm missing something.
If ProcessRequestAsync is implemented correctly, i.e. it is actually non-blocking asynchronous, then yes, it will be truly parallel. The limitations will be dictated by the settings of the associated task scheduler.
If there is no await keyword inside it, it can't be asynchronous, can it?
|
0

If you're getting tired of seeing those compiler warnings, you can tell VS to ignore them.

#pragma warning disable 4014 service.DoSomethingAsync(); #pragma warning restore 4014 

I normally find those warnings helpful so I only disable them on "fire-and-forget" async calls where I don't need to wait for them to finish (e.g., logging).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.