I have the following code inside my c# console application, where i am calling a method called getInfo
in parallel using WhenAll()
method, as follow:-
class Program
{
static int concurrentrequests = int.Parse(ConfigurationManager.AppSettings["ConcurrentRequests"]);
static SemaphoreSlim throttler = new SemaphoreSlim(initialCount: concurrentrequests);
private static ScanInfo getInfo(string website)
{
throttler.Wait();
ScanInfo si = new ScanInfo();
int counter = 1;
try
{
//code goes here..
}
catch (Exception e)
{
//code goes here
}
finally
{
throttler.Release();
}
}
return si;
}
static void Main(string[] args)
{
Marketing ipfd = new Marketing();
try
{
using (WebClient wc = new WebClient()) // call the PM API to get the account id
{
//code goes here
}
}
catch (Exception e)
{
}
var tasks = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
var results = Task.WhenAll(tasks);
//code goes here..
}
}
but i am getting this exception:-
Argument 1: cannot convert from
System.Collections.Generic.List<Sales.ScanInfo> to
System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>
so can anyone advice why i am getting this error?
var tasks = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
var results = Task.WhenAll(tasks);
tasks contains the results. YOu dont have to do any waiting at all. What make syou think you do. So just do
var results = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.