Our team is creating a new workflow system to replace the old one. I was instructed to transfer the old data to a new schema. I decided to do this by creating a small Windows Forms project, because the schema is radically different, and direct TSQL scripts are not an adequate solution.
The main sealed class "ImportController", which does the work, declares the following delegate event:
public delegate void ImportProgressEventHandler(object sender, ImportProgressEventArgs e); public static event ImportProgressEventHandler importProgressEvent;
The main window launches the static method in this class using the new thread:
Thread dataProcessingThread = new Thread(new ParameterizedThreadStart(ImportController.ImportData)); dataProcessingThread.Name = "Data Importer: Data Processing Thread"; dataProcessingThread.Start(settings);
args ImportProgressEvent contains a string message, the maximum int value for the progress bar and the current value of the current performance. The Windows form attributes to the event:
ImportController.importProgressEvent += new ImportController.ImportProgressEventHandler(ImportController_importProgressEvent);
And it responds to the event this way using its 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 list 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 fact is, the ListBox seems to update very quickly, but the progress indicator never moves until the batch is almost complete? what gives?