<p>I'm working on an app that grabs and installs a bunch of updates off an an external server, and need some help with threading. The user follows this process:</p>

<ul>
<li>Clicks button</li>
<li>Method checks for updates, count is returned.</li>
<li>If greater than 0, then ask the user if they want to install using MessageBox.Show().</li>
<li>If yes, it runs through a loop and call BeginInvoke() on the run() method of each update to run it in the background.</li>
<li>My update class has some events that are used to update a progress bar etc. </li>
</ul>

<p>The progress bar updates are fine, but the MessageBox is not fully cleared from the screen because the update loop starts right after the user clicks yes (see screenshot below).</p>

<ul>
<li>What should I do to make the messagebox disappear instantly before the update loop starts?</li>
<li>Should I be using Threads instead of BeginInvoke()?</li>
<li>Should I be doing the initial update check on a separate thread and calling MessageBox.Show() from that thread?</li>
</ul>

<p><strong>Code</strong></p>

<pre><code>// Button clicked event handler code...
DialogResult dlgRes = MessageBox.Show(
    string.Format("There are {0} updates available.\n\nInstall these now?", 
    um2.Updates.Count), "Updates Available", 
    MessageBoxButtons.YesNo, 
    MessageBoxIcon.Question, 
    MessageBoxDefaultButton.Button2
);

if (dlgRes == DialogResult.Yes)
{
    ProcessAllUpdates(um2); 
}

// Processes a bunch of items in a loop
private void ProcessAllUpdates(UpdateManager2 um2)
{
    for (int i = 0; i &lt; um2.Updates.Count; i++)
    {
        Update2 update = um2.Updates[i];

        ProcessSingleUpdate(update);

        int percentComplete = Utilities.CalculatePercentCompleted(i, um2.Updates.Count);

        UpdateOverallProgress(percentComplete);
    }
}

// Process a single update with IAsyncResult
private void ProcessSingleUpdate(Update2 update)
{
    update.Action.OnStart += Action_OnStart;
    update.Action.OnProgress += Action_OnProgress;
    update.Action.OnCompletion += Action_OnCompletion;

    //synchronous
    //update.Action.Run();

    // async
    IAsyncResult ar = this.BeginInvoke((MethodInvoker)delegate() { update.Action.Run(); });
}
</code></pre>

<p><strong>Screenshot</strong></p>

<p><a href="http://www.flickr.com/photos/brianly/2760435228/" rel="nofollow noreferrer" title="Windows Mobile Bug by BrianLy, on Flickr"><img src="https://farm4.static.flickr.com/3043/2760435228_1af7dbf365_o.gif" width="233" height="266" alt="Windows Mobile Bug" /></a></p>

[{'answer_id': 10116, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 1, 'selected': False, 'text': '<p>Have you tried putting a </p>\n\n<pre><code>Application.DoEvents()\n</code></pre>\n\n<p>in here</p>\n\n<pre><code>if (dlgRes == DialogResult.Yes)\n{\n   Application.DoEvents(); \n   ProcessAllUpdates(um2); \n}\n</code></pre>\n'}, {'answer_id': 10157, 'author': 'John Sibly', 'author_id': 1078, 'author_profile': 'https://Stackoverflow.com/users/1078', 'pm_score': 4, 'selected': True, 'text': '<p>Your UI isn\'t updating because all the work is happening in the user interface thread.\nYour call to: </p>\n\n<pre><code>this.BeginInvoke((MethodInvoker)delegate() {update.Action.Run(); }) \n</code></pre>\n\n<p>is saying invoke update.Action.Run() on the thread that created "this" (your form), which is the user interface thread.</p>\n\n<pre><code>Application.DoEvents()\n</code></pre>\n\n<p>will indeed give the UI thread the chance to redraw the screen, but I\'d be tempted to create new delegate, and call BeginInvoke on that. </p>\n\n<p>This will execute the update.Action.Run() function on a seperate thread allocated from the thread pool. You can then keep checking the IAsyncResult until the update is complete, querying the update object for its progress after every check (because you can\'t have the other thread update the progress bar/UI), then calling Application.DoEvents(). </p>\n\n<p>You also are supposed to call EndInvoke() afterwards otherwise you may end up leaking resources</p>\n\n<p>I would also be tempted to put a cancel button on the progress dialog, and add a timeout, otherwise if the update gets stuck (or takes too long) then your application will have locked up forever.</p>\n'}, {'answer_id': 21054, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 1, 'selected': False, 'text': '<p>@ John Sibly</p>\n\n<p><a href="http://www.interact-sw.co.uk/iangblog/2005/05/16/endinvokerequired" rel="nofollow noreferrer">You can get away with <em>not</em> calling EndInvoke when dealing with WinForms without any negative consequences.</a></p>\n\n<blockquote>\n  <p>The only documented exception to the rule that I\'m aware of is in Windows Forms, where you are officially allowed to call Control.BeginInvoke without bothering to call Control.EndInvoke.</p>\n</blockquote>\n\n<p>However in all other cases when dealing with the Begin/End Async pattern you should assume it will leak, as you stated.</p>\n'}]
['https://Stackoverflow.com/questions/10071', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/636/']