1
int countUploadMsg = 0;
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
int counter = 0;
allLoadedMessages = new List<OpenPop.Mime.Message>();
OpenPop.Mime.Message loadedMessage = null;
DirectoryInfo di = new DirectoryInfo(@"e:\test\");
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
loadedMessage = OpenPop.Mime.Message.Load(file);
allLoadedMessages.Add(loadedMessage);
int nProgress = (files.Length - counter + 1) * 100/files.Length;
backgroundWorker2.ReportProgress(nProgress);
counter += 1;
}
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbt1.Value = e.ProgressPercentage;
pbt1.Text = e.ProgressPercentage.ToString() + "%";
pbt1.Invalidate();
lvnf.Items.Add(new ListViewItem(new string[]
{
allLoadedMessages[countUploadMsg].Headers.From.ToString(), //From Column
allLoadedMessages[countUploadMsg].Headers.Subject, //Subject Column
allLoadedMessages[countUploadMsg].Headers.DateSent.ToString() //Date Column
}));
countUploadMsg += 1;
}
變量pbt1
是ProgressBar
。爲什麼progressBar顯示從結尾到開始倒計時的進度?
我看到的是ProgressBar
綠色100%也文本是100%,然後開始向後移動無論是綠色和文本100%99%98%....
或許真的與計算我做:
int nProgress = (files.Length - counter + 1) * 100/files.Length;
而且files.Length
也應該files.Length-1
?
是的,這是你的計算。你的'計數器'正在增加,而你的'files.Length'是不變的。 'nProgress'會越來越少。因此你可以100%降到0%。 – Ian
你的計算是正確的 - 如果你從文件長度減去計數器開始,你顯然會從100%開始。你必須像'int nProgress = counter * 100/files.length'這樣的東西,並在報告之前增加計數器,所以你不需要+1 ... – Marwie