title: Updating a dialog from another form
body: <p>I know there are a lot of similar questions out there and I have read through a lot of them. Unfortunately, I still couldn't solve my problem after reading through them - however I am relatively new to C#. According to <a href="http://msdn.microsoft.com/en-us/library/ms171728.aspx" rel="nofollow noreferrer">docs</a> the problem of not being thread safe results in an <code>InvalidOperationException</code> when using the debugger. This is not the case in my problem.</p>
  
  <p>I've recreated the problem with a simple raw test class to concentrate on my problem.</p>
  
  <p>The main form is supposed to show a kind of a progress dialog.</p>
  
  <pre><code>public partial class ImportStatusDialog : Form
  {
      public ImportStatusDialog()
      {
          InitializeComponent();
      }
  
      public void updateFileStatus(string path)
      {
          t_filename.Text = path;         
      }
  
      public void updatePrintStatus()
      {      
          t_printed.Text = "sent to printer";
      }
  
      public void updateImportStatus(string clientName)
      {
          t_client.Text = clientName;
      }
  
      public void updateArchiveStatus()
      {
          t_archived.Text = "archived";
      }
  }
  </code></pre>
  
  <p>When that code is called without any Invoke() from the main form:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
          ImportStatusDialog nDialog = new ImportStatusDialog();
  
          nDialog.Show();
  
          nDialog.updateFileStatus("test");
          Thread.Sleep(1000);
          nDialog.updateImportStatus("TestClient");
          Thread.Sleep(1000);
          nDialog.updatePrintStatus();
          Thread.Sleep(1000);
          nDialog.updateArchiveStatus();
          Thread.Sleep(1000);
  
          nDialog.Close();
      }
  </code></pre>
  
  <p>And even when I call it like this:</p>
  
  <pre><code>private void button3_Click(object sender, EventArgs e)
      {
          ImportStatusDialog nDialog = new ImportStatusDialog();
  
          nDialog.Show();
  
          if (this.InvokeRequired)
          {
              this.Invoke((MethodInvoker)delegate
              {
                  nDialog.updateFileStatus("Test");
              });
          }
          else
          {
              nDialog.updateFileStatus("Test");
          }
  
          Thread.Sleep(1000);
  
          if (this.InvokeRequired)
          {
              this.Invoke((MethodInvoker)delegate
              {
                  nDialog.updatePrintStatus();
              });
          }
          else
          {
              nDialog.updatePrintStatus();
          }
  
          Thread.Sleep(1000);
  
          if (this.InvokeRequired)
          {
              this.Invoke((MethodInvoker)delegate
              {
                  nDialog.updateImportStatus("cName");
              });
          }
          else
          {
              nDialog.updateImportStatus("cName");
          }
  
          Thread.Sleep(1000);
  
          if (this.InvokeRequired)
          {
              this.Invoke((MethodInvoker)delegate
              {
                  nDialog.updateArchiveStatus();
              });
          }
          else
          {
              nDialog.updateArchiveStatus();
          }
  
          Thread.Sleep(1000);
  
          nDialog.Close();
      }
  </code></pre>
  
  <p>the dialog which looks like this in the designer (in my example)</p>
  
  <p><img src="https://i.stack.imgur.com/qyuS2.png" alt="designer view"></p>
  
  <p>will be displayed like that:</p>
  
  <p><img src="https://i.stack.imgur.com/YmjDd.png" alt="dialog at runtime"></p>
  
  <p>When I use <a href="http://msdn.microsoft.com/de-de/library/c7ykbedk(v=vs.110).aspx" rel="nofollow noreferrer">ShowDialog()</a> instead of <a href="http://msdn.microsoft.com/de-de/library/system.windows.forms.control.show" rel="nofollow noreferrer">Show()</a> the dialog displays correnctly, but as the API Doc points out </p>
  
  <blockquote>
    <p>You can use this method to display a modal dialog box in your
    application. When this method is called, the code following it is not
    executed until after the dialog box is closed</p>
  </blockquote>
  
  <p>which is not what I want, especially as it would mean that the dialog updating would only happen after it has been closed again.</p>
  
  <p>What am I doing wrong here? This seems to be a trivial problem and yet the solution evades me. Please keep in mind that I am new to C# GUI programming.</p>
  
  <p>Also, I would like to ask what would be the right place for using the Invoke()? Do Would you use it in the main form when calling the dialog methods or rather in the dialog update methods itself?</p>