简体   繁体   中英

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. (System.Net sockets aren't really supported in wp8) So I call to make the connection:

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. 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."

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! Any help would be much appreciated.

You need to have your function, the one calling socket.ConnectAsync(e) , asynchronous as well.

void MyFunction()
{
    await socket.ConnectAsync(e);
}

gets you the error you are seeing. Change it to

async Task MyFunction()
{
    await socket.ConnectAsync(e);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM