title: How to create a MFC dialog with a progress bar in a separate thread?
body: <p>My application may take a while to connect to a database. This connection is made with a single library function call, i.e. I cannot put progress updates in there and make callbacks or something similar.  </p>
  
  <p>My idea was to create a dialog with a progress bar in a separate thread before connecting to the DB. This dialog will continually change the progress status with <code>CProgressCtrl::StepIt()</code> so the user sees something happening.<br>
  After that dialog is set up and doing its thing I want to call the DB connection function from the main thread.
  After the connection function completed, I want to stop the progress bar thread.</p>
  
  <p>Let me paint a picture:</p>
  
  <pre><code>CMyApp::       ProgressThread
  InitInstance()      .
      |               .
      |               .
      +-Create Dialog-+
      |               |
      |             Animate
   Connect          Progress
      to             Bar
      DB              |
      |               |
      +-Destroy Dlg---+
      |               .
      |               .
  </code></pre>
  
  <p>Is that possible? If yes, how?</p>
  
  <p>Maybe the whole thing would work using timers, too. Would probably be much simpler but I couldn't get that to work either.</p>
  
  <ol>
  <li>I am aware of <code>CProgressCtrl::SetMarquee()</code> which might do exactly what I need but I can't use it because the application does not have Unicode support.</li>
  <li>I could move the db connection call into a separate thread but that way it looks like a lot of changes to the code and extra handling of connection errors.</li>
  </ol>
  
  <p><strong>Update 2</strong><br>
  I got it working the way AlexEzh and Javier De Pedro suggested: Put the DB stuf into its own thread.<br>
  initially I had concerns about how error handling could be done but it's actually quite similar to how it was before.</p>
  
  <ol>
  <li>In the main thread I create a struct with connection parameters, result flag and thread-running-flag. The latter is initially set to <code>true</code>.</li>
  <li>I create a thread and pass that struct as parameter.</li>
  <li>I create a dialog that displays a progress bar in the main thread.</li>
  <li>Also in the main thread there is a loop that runs while the thread-running-flag is set. It calls <code>CMyDialog::Animate()</code> which calls <code>CProgressCtrl::StepIt()</code> and then <code>Sleep()</code>s a bit.</li>
  <li>The thread executes the db-connection code and sets the running-flag to <code>false</code> when done.</li>
  <li>When the main thread exits the loop it can handle errors exactly as it did before.</li>
  </ol>
  
  <p><strong>Disadvantage:</strong> Moving the mouse over the window doesn't work. It's invisible. Thus no cancel-button or other interactive dialog elements can be used. I can live with that, however.</p>
  
  <p>Since you liked the diagram, here is how it now looks like:</p>
  
  <pre><code>CMyApp::        WorkerThread
  InitInstance()      .
      |               .
      |               .
  Create Dialog       .
      |               .
      +-Start Thread--+
      |               |
      |             Connect
   Animate            to
   Progress           DB
     Bar              |
      |               |
      +-Thread Ends---+
      |               .
   Destroy Dlg        .
      |               .
  </code></pre>