Domanda

I'm using the asyncbridge for 3.5, and have run into a problem. When I have a method returning a void, I know I have to return a Task.

However, the method below doesn't work

public async Task PingAsync()
{
  return await Task.Factory.FromAsync(client.BeginPing, client.EndPing, new object());
}

Works like a charm when I return values, I.E. return await Task.FromFactory....

Am I missing something here?

È stato utile?

Soluzione

I suspect you can just change it to:

public Task PingAsync()
{
    return Task.Factory.FromAsync(client.BeginPing, client.EndPing, new object());
}

There's no benefit in making this an async method - you're just trying to create a task representing the async operation, and TaskFactory.FromAsync does that for you.

If you really want to use async and await, just remove the return part:

public async Task PingAsync()
{
    await Task.Factory.FromAsync(client.BeginPing, client.EndPing, new object());
}

Looking at the overloads for FromAsync I suspect you might want client.BeginPing() instead of client.BeginPing... and it's not clear why you're creating a new object as the state instead of just using null. Presumably you're not using that state...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top