I am using Parallel.invoke
for executing multiple tasks but it returns the response before all the operations are completed. I want to use await keyboard while calling methods as all the methods are async
but it doesn't allow me to do so. Also I know I can use Task.WhenAll()
but the requirement is to use Parallel.Invoke
so is there any way to achieve my goal?
var list = new List<string>();
var myArr = new string[] { "usa", "eng", "fra" };
Parallel.Invoke(() =>
{
myArr.Select(i => {
var data= MyMethod(i);
list.Add(1, data);
});
});
and the MyMethod implementation
public async Task<string> GetName(string country)
{
var data = await _db.GetAll().FindAsync(country).Capital;
return data;
}
Also I have another question is there any better approach to call the method 3 or 4 times inside Parallel.Invoke
?