<p>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.</p>

<p>The main sealed class 'ImportController' that does the work declares the following delegate event:</p>

<pre><code>public delegate void ImportProgressEventHandler(object sender, ImportProgressEventArgs e);
public static event ImportProgressEventHandler importProgressEvent;
</code></pre>

<p>The main window starts a static method in that class using a new thread:</p>

<pre><code>Thread dataProcessingThread = new Thread(new ParameterizedThreadStart(ImportController.ImportData));
dataProcessingThread.Name = "Data Importer: Data Processing Thread";
dataProcessingThread.Start(settings);
</code></pre>

<p>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:</p>

<pre><code>ImportController.importProgressEvent += new ImportController.ImportProgressEventHandler(ImportController_importProgressEvent);
</code></pre>

<p>And responds to the event in this manner using it's own delegate:</p>

<pre><code>    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);
            }
</code></pre>

<p>Finally the progress bar and listbox are updated:</p>

<pre><code>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 &gt;= 0 &amp;&amp; progressMax &gt; 0 &amp;&amp; currentProgress &lt;= progressMax)
            {
                this.ImportProgressBar.Maximum = progressMax;
                this.ImportProgressBar.Value = currentProgress;
            }
        }
</code></pre>

<p>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 ?</p>

[{'answer_id': 12104, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe you can try the BackgroundWorker component. It makes threading easier. Examples here:</p>\n\n<ul>\n<li><a href="http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx" rel="nofollow noreferrer">BackgroundWorker Threads and Supporting Cancel</a></li>\n<li><a href="http://www.c-sharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx" rel="nofollow noreferrer">Using the BackgroundWorker Component in .NET 2 applications</a></li>\n<li><a href="http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html" rel="nofollow noreferrer">BackgroundWorker Sample</a></li>\n</ul>\n'}, {'answer_id': 12109, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': -1, 'selected': False, 'text': "<p>Are you sure that the UI thread is running freely during all this process?  i.e. it's not sitting blocked-up on a Join or some other wait?  That's what it looks like to me.</p>\n\n<p>The suggestion of using BackgroundWorker is a good one - definitely superior to trying to sledge-hammer your way out of the problem with a load of Refresh/Update calls.</p>\n\n<p>And BackgroundWorker will use a pool thread, which is a friendlier way to behave than creating your own short-lived thread.</p>\n"}, {'answer_id': 12115, 'author': 'Peteter', 'author_id': 1192, 'author_profile': 'https://Stackoverflow.com/users/1192', 'pm_score': 0, 'selected': False, 'text': '<p>Maybe outside of the scope but, to sometimes its useful to do an <code>Application.DoEvents();</code> to make the gui parts react to user input, such as pressing the cancel-button on a status bar dialog.</p>\n'}, {'answer_id': 12118, 'author': 'Patrik Svensson', 'author_id': 936, 'author_profile': 'https://Stackoverflow.com/users/936', 'pm_score': 0, 'selected': False, 'text': '<p>Do you by any chance run Windows Vista? I\'ve noticed the exactly same thing in some work related applications. Somehow, there seem to be a delay when the progress bar "animates".</p>\n'}, {'answer_id': 12119, 'author': 'hollystyles', 'author_id': 2083160, 'author_profile': 'https://Stackoverflow.com/users/2083160', 'pm_score': 1, 'selected': True, 'text': "<p>@John</p>\n\n<p>Thanks for the links. </p>\n\n<p>@Will</p>\n\n<p>There's no gain from threadpooling as I know it will only ever spawn one thread. The use of a thread is purely to have a responsive UI while SQL Server is being pounded with reads and writes. It's certainly not a short lived thread.</p>\n\n<p>Regarding sledge-hammers you're right. But, as it turns out my problem was between screen and chair after all. I seem to have an unusal batch of data that has many many many more foreign key records than the other batches and just happens to get selected early in the process meaning the currentProgress doesn't get ++'d for a good 10 seconds. </p>\n\n<p>@All</p>\n\n<p>Thanks for all your input, it got me thinking, which got me looking elsewhere in the code, which led to my ahaa moment of humility where I prove yet again the error is usually human :)</p>\n"}, {'answer_id': 12125, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': -1, 'selected': False, 'text': "<blockquote>\n  <p>There's no gain from threadpooling as\n  I know it will only ever spawn one\n  thread. The use of a thread is purely\n  to have a responsive UI while SQL\n  Server is being pounded with reads and\n  writes. It's certainly not a short\n  lived thread.</p>\n</blockquote>\n\n<p>OK, I appreciate that, and glad you found your bug, but have you looked at BackgroundWorker?  It does pretty much exactly what you're doing, but in a standardised fashion (i.e. without your own delegates) and without the need to create a new thread - both of which are (perhaps small, but maybe still useful) advantages.</p>\n"}]
['https://Stackoverflow.com/questions/12095', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2083160/']