我正在尝试使用以下代码来报告循环字典的进度:
int progressTracker = 0;
foreach (KeyValuePair<string, Func<T>> methodPair in DicitionaryCollection)
{
progressTracker++;
progressTracker = (100 * progressTracker) / DicitionaryCollection.Count;
// do something with progress and collection contents
}
然而,这会继续返回值,就价值而言是一种跳跃。有什么想法吗?
答案 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;
}