请求速度

时间:2012-06-21 01:30:57

标签: c# multithreading task parallel.foreach

我有代码通过两个并行为每个循环发送Web请求。在此方法发生之前添加线程会导致执行这些任务的延迟还是会实现更多的Web请求?

            for (int i = 0; i < threads; i++)
            {
                populateClient(ServerID, debugLog, type, mC);
                tasks[i] = Task.Factory.StartNew(() =>
                {
                   testMaps(ServerID, DebugLog, Type, mC.cl, mC.keywords, mC.locations);
                });
            }
            while (tasks.Any(t => !t.IsCompleted)) { } //spin wait

// ...

  static void testMaps(Int64 serverID, String debugLog, String type, ClientInfo cl,
        List<Keyword> keywords,
        List<String> locations)
    {
        Parallel.ForEach(keywords, keyword =>  
        {
            Parallel.ForEach(locations, location =>
           {
           strFileName = file.DownloadString(query);

// ..

1 个答案:

答案 0 :(得分:1)

你的节目没问题。像这样的I / O可以并行运行并具有相对较高的并行度,因此,通过Parallel.ForEach显式启动许多任务都不是问题。实现通常会很好地管理它们,而不会导致不必要的开销,因为它基于底层线程池。

相关问题