I wrote a simple application that adds 100,000 "Hello World" lines to the list using BackgroundWorker.
Below is the code of the work that my background worker does in a separate thread:
private void BgWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
int min = 0;
foreach (var hw in hwList)
{
Thread.Sleep(1);
int progress = Convert.ToInt32((Double)min / hwList.Count * 100);
min++;
bgWorker.ReportProgress(progress);
}
}
private void BgWorkerOnProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs)
{
ProgressBar.Value = progressChangedEventArgs.ProgressPercentage;
}
Everything works fine, except that when you uninstall Thread.Sleep (1), BackgroundWorker no longer reports progress. (I suppose it needs some time). Pausing a flow for 1 ms actually makes BackgroundWorker a progress report, but it is very slow.
My question is, is there a way to get rid of the sleeping thread, but at the same time do the BackgroundWorker progress report correctly?
, BackgroundWorker , , , .