I have case there i want to call one asyn method inside paralle.Foreach loop
public void ItemCheck<T>(IList<T> items,int id) { Parallel.ForEach(items, (current) => { PostData(current,id); }); Console.log("ItemCheck executed") } public async void PostData<T>(T obj, int id) { Console.lgo("PosstData executed") } Output :
ItemCheck executed PosstData executed Why it happens like this ?? Before completing execution of PostData method,next line is executed.How can i solve this issue.Anyone help on this
Parallel.ForEachseems pointless when you're using async methods - just call all the async methods, then wait for all the tasks to complete (when you've changed your async method to returnTask)Task.Delayto emulate actual work.Before completing execution of PostData, that is exactly asynchronous, you cannot and should not useasyncinParallel.ForEachwhich is designed only for synchronous methods.