I saw a lot of code snippets like here:
TcpClient client = await listener.AcceptTcpClientAsync();
Logger.Info("Client accepted");
https://codereview.stackexchange.com/questions/33748/wpf-async-await-tcpclient-tcplistener-sample
var tcpClient = await listener.AcceptTcpClientAsync();
HandleConnectionAsync(tcpClient);
https://bsmadhu.wordpress.com/2012/09/29/simplify-asynchronous-programming-with-c-5-asyncawait/
It seems like it works for the most of people. Unfortunately, it doesn't work for me -- compiler gives me the following error:
error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'
What am I doing wrong? How can I fix it?
What am I doing wrong?
Exactly as the error states, the calling method needs to be marked with the async
modifier and have a return type of Task
or Task<T>
:
public async Task ConnectAsync()
{
TcpClient client = await listener.AcceptTcpClientAsync();
Logger.Info("Client accepted");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.