C#,对于回路内部并联,对于回路工作不正常

C#,对于回路内部并联,对于回路工作不正常,c#,for-loop,webforms,parallel-for,C#,For Loop,Webforms,Parallel For,这是我的代码,工作不正常 从0到UrlList计数的循环正常。大约1500-2000年 每10次循环后,控制会话。如果不存在或超时,则转到并刷新。这是第一个并联回路工作正常。 i=10,x=0到9之间 后来,平行线也不起作用了。我正在看x的“添加手表”。 x没有改变。第一个循环中的最后一个数字保持不变 我能做什么 TokenController control = new TokenController(); for (int i = 0; i < UrlList.Count; i++)

这是我的代码,工作不正常

从0到UrlList计数的循环正常。大约1500-2000年

每10次循环后,控制会话。如果不存在或超时,则转到并刷新。这是第一个并联回路工作正常。 i=10,x=0到9之间

后来,平行线也不起作用了。我正在看x的“添加手表”。 x没有改变。第一个循环中的最后一个数字保持不变

我能做什么

TokenController control = new TokenController();
for (int i = 0; i < UrlList.Count; i++)
{
    if(control.SessionControl(false, 0))
    {
       Parallel.For(i, 10, x => {
          //HttpRequest

       });

       i += 9;
    }
}
TokenController control=new-TokenController();
for(int i=0;i{
//HttpRequest
});
i+=9;
}
}

并行的第二个参数。的第二个参数是“to”(独占)值,而不是“重复次数”:


似乎你对射程有问题;取决于你想要什么

 for (int i = 0; i < UrlList.Count; i++) {
   // at 0th, 10th, 20th ... N * 10 ... position 
   if (i % 10 == 0) {
     // Control session: 
     // HttpRequest ...
   }
 } 
for(int i=0;i

int-step=10;
对于(int i=0;i{
//HttpRequest
});
i+=(步骤-1);
}
}

并行。对于(i,i+10,x=>{…}
?当你在
i
-第十个位置时,你想要
10
循环更多,而不是从
i
10
@DmitryBychenko脱离主题,但你的大脑中是否有适合的编译器?@Lucifer:典型错误在典型位置:当
循环
不执行时,看看条件(上下文中的范围)@路西法在这里拥有成千上万的声誉时,这被称为经验。因此,它给我的主要好处是,只要看一段代码,就可以非常快速地分析代码的错误。@PatrickHofman我需要走得更远,还需要学到很多东西,但感谢像你这样的人,我可以学到很多谢谢你。你节省了我的时间。
Parallel.For(i, i + 10, x => {
   //HttpRequest

});
 for (int i = 0; i < UrlList.Count; i++) {
   // at 0th, 10th, 20th ... N * 10 ... position 
   if (i % 10 == 0) {
     // Control session: 
     // HttpRequest ...
   }
 } 
 int step = 10;

 for (int i = 0; i < UrlList.Count; ++i) {
   // Control session can appear at any moment, indepent on i 
   if (control.SessionControl(false, 0)) {
     // When we at i-th postion we want 10 loops more: i + step
     // not from i to step
     Parallel.For(i, i + step, x => {
      //HttpRequest
     });

     i += (step - 1);
   }
 }