无法从System.Collections.Generic.List <sales.scaninfo>转换为System.Collections.Generic.IEnumerable <system.threading.tasks.task>

时间:2018-11-29 23:40:18

标签: c# .net parallel-processing

我的c#控制台应用程序中包含以下代码,其中我使用getInfo方法并行调用一个名为WhenAll()的方法,如下所示:-

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..   
        }       
    }

但是我遇到了这个例外:-

Argument 1: cannot convert from
System.Collections.Generic.List<Sales.ScanInfo> to
System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>

任何人都可以建议我为什么收到此错误吗?

1 个答案:

答案 0 :(得分:1)

  var tasks = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
  var results = Task.WhenAll(tasks);

任务包含结果。你根本不需要做任何等待。是什么让您认为自己做得到。所以就做

  var results  = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();