Windows Forms Threading and Events – ListBox updates promptly but progressbar experiences huge delay

Question :

Windows Forms Threading and Events – ListBox updates promptly but progressbar experiences huge delay,

Answer :

Our team is creating a new recruitment workflow system to replace an old one. I have been tasked with migrating the old data into the new schema. I have decided to do this by creating a small Windows Forms project as the schema are radically different and straight TSQL scripts are not an adequate solution.

The main sealed class ‘ImportController’ that does the work declares the following delegate event:

public delegate void ImportProgressEventHandler(object sender, ImportProgressEventArgs e);  public static event ImportProgressEventHandler importProgressEvent;  

The main window starts a static method in that class using a new thread:

Thread dataProcessingThread = new Thread(new ParameterizedThreadStart(ImportController.ImportData));  dataProcessingThread.Name = "Data Importer: Data Processing Thread";  dataProcessingThread.Start(settings);  

the ImportProgressEvent args carries a string message, a max int value for the progress bar and an current progress int value. The Windows form subcribes to the event:

ImportController.importProgressEvent += new ImportController.ImportProgressEventHandler(ImportController_importProgressEvent);  

And responds to the event in this manner using it’s own delegate:

    private delegate void TaskCompletedUIDelegate(string completedTask, int currentProgress, int progressMax);    private void ImportController_importProgressEvent(object sender, ImportProgressEventArgs e)              {                  this.Invoke(new TaskCompletedUIDelegate(this.DisplayCompletedTask), e.CompletedTask, e.CurrentProgress, e.ProgressMax);              }  

Finally the progress bar and listbox are updated:

private void DisplayCompletedTask(string completedTask, int currentProgress, int progressMax)          {              string[] items = completedTask.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);                foreach (string item in items)              {                  this.lstTasks.Items.Add(item);              }                if (currentProgress >= 0 && progressMax > 0 && currentProgress <= progressMax)              {                  this.ImportProgressBar.Maximum = progressMax;                  this.ImportProgressBar.Value = currentProgress;              }          }  

The thing is the ListBox seems to update very quickly, but the progress bar never moves until the batch is almost complete anyway ??? what gives ?

Read More  Custom WPF command pattern example


Maybe you can try the BackgroundWorker component. It makes threading easier. Examples here:

That's the answer Windows Forms Threading and Events – ListBox updates promptly but progressbar experiences huge delay, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Do you name controls on forms using the same convention as a private variable?