I guess you work with WinForms. The Invoke
method lets you to run code in the UI thread. But, why do you run the whole of the worker's code (including the Sleep
statement that blocks...) in the UI thread? Try to run only the progress change using the Invoke
method. Try to remove the Invoke
call from DoWork
and, move the whole of the UI changes to the backgroundWorker_ProgressChanged
method (see the comment below). Something like:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100;i++ )
{
Thread.Sleep(1000);
backgroundWorker.ReportProgress(i);
}
backgroundWorker.ReportProgress(100);
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
txtLog.Text = e.ProgressPercentage.ToString();
progressBar1.Value = e.ProgressPercentage;
}