Note that there are some explanatory texts on larger screens.

plurals
  1. POShowing a please wait animation while UI is populated
    text
    copied!<p>I'm writing a WinForms window in C# which displays about 12 <code>ListBox</code> and 6 <code>ComboBox</code> controls each with a few hundred to a few thousand items.</p> <p>It takes a little while to populate these. Not a long while - just a few seconds, but it's nice for the user to have something to look at so they know the program is working away in the background while they wait.</p> <p>I have a generic "Please Wait" animated borderless top-most window which appears while this happens, however I'm having trouble with the animation.</p> <p>For most tasks which take a little while, I solve this in the following way:</p> <pre><code>Program.ShowPleaseWait(); // Show top-most animation Thread t = new Thread(stuffToDo); // Run stuffToDo() in separate thread t.Start(); While (t.IsAlive) Application.DoEvents(); // Run message queue, necessary for animation Program.HidePleaseWait(); // Hide top-most animation </code></pre> <p>and it works quite well. Occasionally the stuff in the thread will need to Invoke something and that sometimes causes a small hiccup in the animation, but it's generally not a big deal.</p> <p>With this form, however, the <i>entire</i> code in the thread is UI code: populating ListBoxes and ComboBoxes. That means everything would have to be enclosed with Invoke blocks, which doesn't make any sense because then there's no point in having it run in a separate thread in the first place.</p> <p>Aside from scrapping the whole worker thread for this particular case and throwing in an <code>Application.DoEvents()</code> every hundred or so insertions in each loop, is there anything I can do to allow the working animation to continue while the controls are populated?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload