2012-06-21 69 views
0

我有通过两个并行为每个循环发送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

你的程序是好的。像这样的I/O可以以相对高的并行度程度并行运行,因此,启动许多任务,明确以及通过Parallel.ForEach都不是问题。实现通常会很好地管理它们,并且不会产生不必要的开销,因为它基于底层线程池。

+0

很酷的感谢,我担心,我正在创造更多的开销,这可能会减慢过程。 –