GettingStarted
GettingStarted

Reputation: 7605

How can I use Tasks correctly?

private async Task<List<PingReply>> PingAsync()
{
    Ping pingSender = new Ping();
    var tasks = serverNames.Select(ip => pingSender.SendPingAsync(ip, 2000));
    var results = await Task.WhenAll(tasks);

    return results.ToList();
}

My question is how would I execute this method?

I've tried

 List<string> data = PingAsync();

But I get this error message

Error   CS0029  Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<System.Net.NetworkInformation.PingReply>>' to 'System.Collections.Generic.List<string>' ServerManager   

I am trying to ping servers and update the UI so we can monitor servers.

I've also tried these

        Task<PingReply> longRunningTask = PingAsync();
        PingReply result = await longRunningTask;

Error

Severity Code Description Project File Line Suppression State 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'. ServerManager

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Threading.Tasks.Task' ServerManager

Upvotes: 1

Views: 2208

Answers (3)

Sean Munson
Sean Munson

Reputation: 361

Given this Async function

private async Task<List<PingReply>> PingAsync()
{
    Ping pingSender = new Ping();
    var tasks = serverNames.Select(
            ip => pingSender.SendPingAsync(ip, 2000)
    );
    var results = await Task.WhenAll(tasks);
    return results.ToList();
}

From some other part of the object, call, accepting the TASK wrapped value

 Task<List<PingReply>> foo = PingAsync();

Then Manually wait it out ...

foo.Wait();

Then Extract the Value

List<PingReply> bar = foo.Result;

Upvotes: 0

pmbanka
pmbanka

Reputation: 1962

Look at your PingAsync method signature. What does it return? Task<List<PingReply>>. Adding async keyword does not change that type, let's say that it basically allows you to use await inside the method.

So, by calling PingAsync, you get an object of type Task<List<PingReply>>, and try to assign it to a List<string> reference - which gives you a type mismatch error.

But, you might ask, how to get an actual T form Task<T>? You should simply use await, just like you do inside your method where you call Task.WhenAll.

List<PingReply> data = await PingAsync();

Upvotes: 4

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149528

The error message by itself is pretty clear. The method signature returns a Task<List<PingReply>>, yet for some reason you expect it to return a List<string> or Task<PingReply>, not sure why.

You should note that starting using async in your code makes it spread like a plague. This means that higher level methods calling async methods will usually need themselves be async and return a Task or Task<T>.

Upvotes: 1

Related Questions