我想在WPF中實現一個進度條。我正在遍歷文件路徑列表(List)並對這些文件執行一些操作。我想跟蹤過程的進度,但它無法正常工作。在這種情況下,每次調用方法tagAndMoveFiles()時,都會使用來自foreach列表中的相同項目調用它,但它被稱爲正確的次數。我對代表不太瞭解(顯然)......我已經包含了所有相關的代碼。謝謝你的幫助。 (上傳進度是進度條)在foreach循環中的委託和調度器不能訪問當前項目
uploadProgress.Maximum = impJob.SourceFilePaths.Count;
DispatcherTimer timer = new DispatcherTimer();
int progress = 0;
foreach (string sourcefilepath in impJob.SourceFilePaths)
{
Thread t = new Thread(new ThreadStart(
delegate()
{
uploadProgress.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(
delegate
{
tagAndMoveFiles(sourcefilepath, targetFolder, impJob, sourceFileProcessed);
uploadProgress.Value = ++progress;
Thread.Sleep(100);
}
));
}
));
t.Start();
}