Skip to main content
 首页 » 编程设计

c#之根据字典集合大小增加进度指示器

2024年02月01日28bhlsheji

我正在尝试找出一种方法来报告使用以下代码循环遍历字典的进度:

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

然而,这会不断返回值,在值方面有点跳跃。有什么想法吗?

请您参考如下方法:

使用不同的变量。

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 
}