I want to create within a function a named lambda function, so that I can call it repeatedly afterwards in the same function.
I used to do this synchronously/without tasks with
Func<string, bool> pingable = (url) => return pingtest(url);
but in this case I want to call the pingable function as a task, so I would need a Task return type.
This is where I am stuck.
For all below, I am getting compile errors:
* Func<string, Task<bool>> pingable = (input) => { return pingtest(url); };
* Task<bool> pingable = new Task<bool>((input) => { return pingtest(url); });
I can declare the function normally though, but then I cannot call it as a task:
Func<string, bool> pingable = (input) => { return pingtest(url); };
var tasks = new List<Task>();
* tasks.Add(async new Task(ping("google.de")));
All the lines I have marked with a * produce copmile errors.
http://dotnetcodr.com/2014/01/17/getting-a-return-value-from-a-task-with-c/ seems to have a hint at a solution, but the sample there does not allow for not provided input parameters. (Sample taken from there and simplified:)
Task<int> task = new Task<int>(obj =>
{
return obj + 1;
}, 300);
How to create and call named Task lambdas in C#, and I would like to declare them on a function rather than class level.
I want the named lambda in order to call it multiple times (several urls in this case).
Edit/update since you asked for code:
Func<string, Task<bool>> ping = url => Task.Run(() =>
{
try
{
Ping pinger = new Ping();
PingReply reply = pinger.Send(url);
return reply.Status == IPStatus.Success;
}
catch (Exception)
{
return false;
}
});
var tasks = new List<Task>();
tasks.Add(ping("andreas-reiff.de"));
tasks.Add(ping("google.de"));
Task.WaitAll(tasks.ToArray());
bool online = tasks.Select(task => ((Task<bool>)task).Result).Contains(true);
This already makes use of the solution proposed here.
pingtest
looks? – Ghee