【问题标题】:How to limit the number of cycles of a loop under some condition?如何在某些条件下限制循环的循环数?
【发布时间】:2014-01-29 09:56:19
【问题描述】:

我做了一个这样的循环:

    int total;
    total = ((toVal - fromVal) + 1) * 2;
    RadProgressContext progress = RadProgressContext.Current;
    progress.Speed = "N/A";

    finYear = fromVal;

    for (int i = 0; i < total; i++)
    {
          decimal ratio = (i * 100 / total);

            progress.PrimaryTotal = total;
            progress.PrimaryValue = total;
            progress.PrimaryPercent = 100;

            progress.SecondaryTotal = 100; // total;
            progress.SecondaryValue = ratio;//i ;
            progress.SecondaryPercent = ratio; //i;


            progress.CurrentOperationText = "Step " + i.ToString();
            if (!Response.IsClientConnected)
            {
                //Cancel button was clicked or the browser was closed, so stop processing
                break;
            }

            progress.TimeEstimated = (total - i) * 100;
            //Stall the current thread for 0.1 seconds
            System.Threading.Thread.Sleep(100);


    }

现在我想根据toVal &amp; fromVal 运行一个特定的方法 在前一个循环中,但循环次数不同 我想像这样循环运行它:

   for (fromVal; fromVal < toVal  ; fromVal++)
    {
        PrepareNewEmployees(calcYear, fromVal);
    }

例如:

fromVal =  2014 
toVal   = 2015 

所以我想跑两次而不是四次!像这样:

PrepareNewEmployees(calcYear, 2014);
PrepareNewEmployees(calcYear, 2015);

但在上一个循环中for (int i = 0; i &lt; total; i++)

【问题讨论】:

  • 在计算total = ((toVal - fromVal) + 1) * 2时为什么要做* 2
  • 那么for (fromVal; fromVal &lt; toVal ; fromVal++) 有什么问题?
  • @RoyDictus :因为某些方法在(一月,七月)的财政年度运行两次,例如如果toVal = 2015 , fromVal = 2014 然后我有7-2014 1-2015 7-2015 1-2016
  • 我不确定我是否理解 - 为什么不能在第一个循环中嵌套另一个循环?
  • 我真的不明白你的意图。能不能换个说法解释一下?

标签: c# asp.net linq loops for-loop


【解决方案1】:

您错过了进度条更新的要点。您不应该运行 4 次迭代并每 2 次迭代做一些工作,而是相反。做一个循环:

 for (int i = fromVal; i < toVal; i++)
{
    PrepareNewEmployees(...);
    decimal ratio = ((double)toVal-i)/(toVal-fromVal) *100;
    //Some other things, that need to be done twice in an iteration
}

【讨论】:

    【解决方案2】:

    由于您已经在使用Thread,请考虑实施以下操作:

    public void ResetProgress()
    {
        SetProgress(0);
    }
    
    public SetProgress(int percents)
    {
        // set progress bar to a given percents/ratio
        // you will have to use Invoke and blablabla
    }
    

    然后任何你的工作将看起来像这样

    ResetProgress();
    // note: you need to remember from which value you start to be able to calculate progress
    for (int i = startVal; i < toVal  ; i++)
    {
        PrepareNewEmployees(calcYear, i);
        SetProgress(100 * (i - startVal) / (toVal - startVal)); // in percents [0-100]
    }
    // optional, required if you exit loop or use suggestion below
    SetProgress(100);
    

    您还可以对其进行优化,不要在每一步之后更新进度,而是在一定数量的步骤之后。例如,不要调用SetProgress,而是这样做

    if(i % 10 == 0)
        SetProgress();
    

    这将使调用SetProgress 的频率减少十倍。当然,也有一些假设,比如:i 从 0 开始,如果你想在末尾有 100% 条,那么i 应该可以被 10 整除。只是一个开始的想法。

    【讨论】:

      猜你喜欢
      • 2011-05-27
      • 2020-10-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2019-09-21
      相关资源
      最近更新 更多