Home How can I use TaskFactory.StartNew<Task<Result>> with async methods?
Reply: 0

How can I use TaskFactory.StartNew<Task<Result>> with async methods?

user1254 Published in March 23, 2018, 8:59 am

I have an async Func<> where I "await" for a async method. I want to make a lot of parallel calls to that Func<> using TaskFactory.StartNew, like the below code, but I get the followin error:

error converting 'System.Threading.Tasks.Task<System.Threading.Tasks.Task<Result>>' to 'System.Threading.Tasks.Task<Result>'

How can I have a parallel calls for a async method?

Func<object, Task<Result>> action = async (object m) =>
        return await Send<Message, Result>(m as Message);
    catch (Exception exc)
        Result result = new Result();
        (result as ResponseBaseDto).Success = false;
        (result as ResponseBaseDto).ErrorList = new List<Entities.Common.ErrorDto>()
            new Entities.Common.ErrorDto{ Code = 9999, Message = exc.Message }
        return result;

// Error on request.ForEach
request.ForEach(r => sending.Add(taskFactory.StartNew<Task<Result>>(action, r)));
var tareas = sending.ToArray();
await Task.WhenAll(tareas);
return tareas.Select(s => s.Result).ToList();

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.46768 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO