Обновления индикатора выполнения параллельных задач задерживаются по сравнению с завершением задачи. ⇐ C#
Обновления индикатора выполнения параллельных задач задерживаются по сравнению с завершением задачи.
I'm working with a progress bar in a C#/.NET application using Windows Forms that is updated from parallel running tasks. Each task invokes a change of the value by 1 on the progress bar exactly as many times as there are steps in the progress bar. However, even though the tasks are always finished promptly, there's a delay of approximately 0.5 to 1 second before the progress bar is completed.
Form File
var progress = new Progress(); progress.ProgressChanged += (_, value) => { progressBarUploading.Value = value; }; Other File
internal async Task UploadProject(IProgress progress) { List listOne = new(); foreach (var item in Project.items) { //do some stuff Interlocked.Increment(ref counter); progress.Report(counter); listOne.Add(Task.Run(() => UploadItems(item, progress))); } // Waits until all the Tasks are finished await Task.WhenAll(listOne); progress.Report(Project.itemCountTotal); } internal async Task UploadItems(AzureDevOpsPOSTitem item, IProgress progress) { List listTwo = new(); foreach (var newItem in item.list) { // do some other stuff Interlocked.Increment(ref counter); progress.Report(counter); listTwo.Add(Task.Run(() => UploadItems(newItem, progress))); } // Waits until all the Tasks are finished. await Task.WhenAll(listTwo); } I suspect that this delay might be due to the UI thread handling the progress updates while the tasks complete on separate threads. Is there a way to ensure that the progress bar updates in sync with the completion of the tasks, without this delay?
Additionally, I've attempted to set the progress bar to 100% after all tasks are completed using progress.Report(maxLength);, but it doesn't seem to work as expected. The progress bar continues to increment step by step (presumably due to the progress.Report calls from the completed tasks). Is there a way to set the progress bar immediately to 100% after all tasks have been completed and stop the unexecuted progress.Reports?
Источник: https://stackoverflow.com/questions/780 ... completion
I'm working with a progress bar in a C#/.NET application using Windows Forms that is updated from parallel running tasks. Each task invokes a change of the value by 1 on the progress bar exactly as many times as there are steps in the progress bar. However, even though the tasks are always finished promptly, there's a delay of approximately 0.5 to 1 second before the progress bar is completed.
Form File
var progress = new Progress(); progress.ProgressChanged += (_, value) => { progressBarUploading.Value = value; }; Other File
internal async Task UploadProject(IProgress progress) { List listOne = new(); foreach (var item in Project.items) { //do some stuff Interlocked.Increment(ref counter); progress.Report(counter); listOne.Add(Task.Run(() => UploadItems(item, progress))); } // Waits until all the Tasks are finished await Task.WhenAll(listOne); progress.Report(Project.itemCountTotal); } internal async Task UploadItems(AzureDevOpsPOSTitem item, IProgress progress) { List listTwo = new(); foreach (var newItem in item.list) { // do some other stuff Interlocked.Increment(ref counter); progress.Report(counter); listTwo.Add(Task.Run(() => UploadItems(newItem, progress))); } // Waits until all the Tasks are finished. await Task.WhenAll(listTwo); } I suspect that this delay might be due to the UI thread handling the progress updates while the tasks complete on separate threads. Is there a way to ensure that the progress bar updates in sync with the completion of the tasks, without this delay?
Additionally, I've attempted to set the progress bar to 100% after all tasks are completed using progress.Report(maxLength);, but it doesn't seem to work as expected. The progress bar continues to increment step by step (presumably due to the progress.Report calls from the completed tasks). Is there a way to set the progress bar immediately to 100% after all tasks have been completed and stop the unexecuted progress.Reports?
Источник: https://stackoverflow.com/questions/780 ... completion
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение