Mehmet
Mehmet

Reputation: 78

async/await - Getting error "Cannot implicitly convert type 'void' to 'Windows.Foundation.IAsyncAction'"

I'm currently trying to understand how the new keywords in c#5 is working with an example. I want send through a socket connection a message and catch the answar with a listener. Where i'm realy stuck is the point that i can't await a method, here is an example:

    private async void SubmitMessage(string strMessage)
    {
        try
        {
            using (StreamSocket objSocket = new StreamSocket())
            {
                IAsyncAction objAction = await objSocket.ConnectAsync(new HostName(TargetHostname), TargetPortservice);
                objAction.Completed = delegate(IAsyncAction asyncAction, AsyncStatus asyncStatus)
                {
                    BindListener(objSocket.Information.LocalPort, objSocket, strMessage);
                };
            }
        }
        catch (Exception objException)
        {
            Debug.WriteLine(objException.Message);
            throw;
        }
    }

Do anyone have an idea how to get this awaited? If i remove 'await' the syntax is correct. Thanks for any help.

Upvotes: 1

Views: 1984

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456607

When you use await, you don't have to muck around with IAsyncAction at all, so something like this should work:

private async Task SubmitMessage(string strMessage)
{
    try
    {
        using (StreamSocket objSocket = new StreamSocket())
        {
            await objSocket.ConnectAsync(new HostName(TargetHostname), TargetPortservice);
            BindListener(objSocket.Information.LocalPort, objSocket, strMessage);
        }
    }
    catch (Exception objException)
    {
        Debug.WriteLine(objException.Message);
        throw;
    }
}

Upvotes: 2

spender
spender

Reputation: 120480

Don't know much about this but would this do?

IAsyncAction objAction = 
 objSocket.ConnectAsync(new HostName(TargetHostname), TargetPortservice);
await objAction;
//...

Upvotes: 0

Related Questions