[英]Unable to use 'await' in a non-'async' method
I'm developing for Windows phone 8, and am trying to connect to a server using the Windows.Networking namespace. 我正在为Windows Phone 8开发,并尝试使用Windows.Networking命名空间连接到服务器。 (System.Net sockets aren't really supported in wp8) So I call to make the connection: (wp8并不真正支持System.Net套接字)所以我打电话建立连接:
socket.ConnectAsync(e);
And I get the error "Because this call is not awaited, execution of the..." which is fair enough, given that the await keyword should be used here. 而且我收到错误消息“因为未等待此调用,执行...”,这是很公平的,因为应该在此处使用await关键字。 However, when I add this in: 但是,当我在其中添加:
await socket.ConnectAsync(e);
I get the error: "the 'await' operator can only be used within an async method." 我收到错误消息:“'await'运算符只能在异步方法中使用。”
This has been more than somewhat frustrating. 这令人沮丧。 I can't really fiddle around with how this works on the method that I'm calling, as this is a precompiled function, with the signature: 我真的不能弄清楚它如何在我正在调用的方法上工作,因为这是一个带有签名的预编译函数:
public IAsyncAction ConnectAsync(EndpointPair endpointPair);
What should I do here? 我该怎么办? In my limited understanding this seems to be a problem with .net, and is driving me mad! 以我的有限理解,这似乎是.net的问题,这使我发疯! Any help would be much appreciated. 任何帮助将非常感激。
You need to have your function, the one calling socket.ConnectAsync(e)
, asynchronous as well. 你需要有你的功能,一个叫socket.ConnectAsync(e)
异步以及。
void MyFunction()
{
await socket.ConnectAsync(e);
}
gets you the error you are seeing. 让您看到的错误。 Change it to 更改为
async Task MyFunction()
{
await socket.ConnectAsync(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.