基于字典集合大小增加进度指标

时间:2013-11-23 18:51:58

标签: c# func

我正在尝试使用以下代码来报告循环字典的进度:

int progressTracker = 0;
foreach (KeyValuePair<string, Func<T>> methodPair in DicitionaryCollection)
{
progressTracker++;
progressTracker = (100 * progressTracker) / DicitionaryCollection.Count;
// do something with progress and collection contents
}

然而,这会继续返回值,就价值而言是一种跳跃。有什么想法吗?

2 个答案:

答案 0 :(得分:4)

使用其他变量。

int currentProgress= 0;
foreach (KeyValuePair<string, Func<T>> methodPair in DicitionaryCollection)
{
  currentProgress++;
  int progressTracker = (100 * currentProgress) / DicitionaryCollection.Count;
  // do something with progress and collection contents
}

答案 1 :(得分:1)

你可以使用for循环 - 注意所需的强制转换为(double):

for (int i = 0; i < DicitionaryCollection.Count; i++)
{
    var methodPair = DicitionaryCollection[i];
    //... Do stuff
    var progressPercent = (i /(double)DicitionaryCollection.Count) * 100;
}