title: Why would this progress bar code cause the progress bar to "hang"?
body: <p>In the legacy code I'm working on, there are several file retrieval steps - first this set of data, then that set of data, which update a progress bar with a label displaying which portion of the process is currently active, and the progress bar itself, of course, updates its position. One portion of all this is hanging, though, and via the use of <code>MessageBox.Show()</code>s (I have to do it this way, can't step through it in the debugger), I've narrowed down where the hanging is occurring, but can't figure out <em>why</em> it's occurring.</p>
  
  <p>Warning: the following code is unorthodox and may well warrant such warning signage as "Here be Dragons" or "This way lies madness." Proceed at your own risk/beware of the peril.</p>
  
  <pre><code>    MessageBox.Show("Made it just before the pbDialog code");//&lt;-- it hangs after this is displayed
      using (pbDialog = new pbDialogs())
      {
          ProgressBar = new frmProgress( this, true);
          ProgressBar.SetProgressLabelText("Vendor/Dept/Expense Data");
          typeProgress = (int)ProgressStates.ProgressQRY;
  
          ProgressBar.label1.Text += " (Receiving)";
          if( pbDialog != null )
          {
              pbDialog.ShowDialog( ProgressBar, this );
          }
          else
          {
              ProgressBar.ShowDialog();
          }
          ProgressBar = null;
          evt.Set();  
      }
      MessageBox.Show("Made it just after the pbDialog code"); //This is not seen
  </code></pre>
  
  <p><code>pbDialog</code> is declared in the same form as this code snippet:</p>
  
  <pre><code>public pbDialogs pbDialog;
  </code></pre>
  
  <p><code>pbDialogs</code> is a class in another form (frmProgress.cs):</p>
  
  <pre><code>public class pbDialogs : IDisposable 
  </code></pre>
  
  <p><code>ProgressBar</code> is an instance of the anonymous class defined in frmProgress.cs (frmProgress, that is to say, which derives from System.Windows.Forms.Form)</p>
  
  <pre><code>public  static  frmProgress ProgressBar;
  </code></pre>
  
  <p><code>typeProgress</code> is a locally defined int:</p>
  
  <pre><code>public static int typeProgress = 0;
  </code></pre>
  
  <p><code>evt</code> is the name of the arg passed into the method from which this snippet originates:</p>
  
  <pre><code>private void FetchVendorDepartmentData(ManualResetEvent evt)
  </code></pre>
  
  <p><code>ManualResetEvent</code>, as you may know, is a member of <code>System.Threading</code></p>
  
  <p>Does anybody see anything eyebrow-raising here (besides the general unorthodoxy of it all)?</p>
  
  <h2>UPDATE</h2>
  
  <p>I added more messages:</p>
  
  <pre><code>    MessageBox.Show("Made it just before the pbDialog code");//&lt;-- it hangs after this. TODO: Remove before deploying
      using (pbDialog = new pbDialogs())
      {
          MessageBox.Show("Made it just before ProgressBar = new frmProgress");// TODO: Remove
          ProgressBar = new frmProgress( this, true);
          MessageBox.Show("Made it just after ProgressBar = new frmProgress");// TODO: Remove
          ProgressBar.SetProgressLabelText("Vendor/Dept/Expense Data");
          typeProgress = (int)ProgressStates.ProgressQRY;
          MessageBox.Show("Made it just after assignment to typeProgress");// TODO: Remove
          ProgressBar.label1.Text += " (Receiving)";
          if( pbDialog != null )
          {
              MessageBox.Show("pbDialog was not null");// TODO: Remove
              pbDialog.ShowDialog( ProgressBar, this );
          }
          else
          {
              MessageBox.Show("pbDialog was null");// TODO: Remove
              ProgressBar.ShowDialog();
          }
          ProgressBar = null;
          MessageBox.Show("ProgressBar set to null");// TODO: Remove
          evt.Set();  
          MessageBox.Show("evt.Set called");// TODO: Remove
      }
      MessageBox.Show("Made it just after the pbDialog code");//TODO: Remove
  }
  </code></pre>
  
  <p>...and the last one I see is, "pbDialog was not null"</p>
  
  <h2>UPDATE 2</h2>
  
  <p>In accord with the answer from "500 - Internal Server Error," I prepended a line to show the ProgressBar:</p>
  
  <pre><code>ProgressBar.ShowDialog(); 
  if( pbDialog != null ) . . .
  </code></pre>
  
  <p>...but it makes no diff; in fact, with that I don't even make it as far as without it - I don't see the "pbDialog was not null" message.</p>
  
  <p>Apologies in advance to Billy Blake, but: What the hammer? What the chain? What the anvil? What dread grasp is going on here?</p>
  
  <h2>UPDATE 3</h2>
  
  <p>So apparently either of these lines cause the hang:</p>
  
  <pre><code>ProgressBar.ShowDialog(); // with this, "pbDialog was not null" is not seen
  
  pbDialog.ShowDialog( ProgressBar, this ); // if make it to here (line above commented out), "ProgressBar set to null" is not seen.
  </code></pre>
  
  <h2>UPDATE 4</h2>
  
  <p>The problem may not be in this code after all, as I found another spot in the same class that uses the exact same code, and that portion of the data retrieval completes just fine...</p>