title: WinForms - Text Not Being Set or Set After Sleep
body: <p>I've just recently started learning how to develop Windows Forms applications. I've been programming in C# for a couple years now (within Unity3D).</p>
  
  <p>I am currently developing an HMI (Human Machine Interface) application that connects to an Arduino. I already have it working; ie: I can connect to it and read from/write to the Arduino. So, this following code WORKS (as far as communication is concerned).</p>
  
  <pre><code>private void ConnectToArduino () {
      labelMonitorConnectionStatus.Text = "Connecting...";
  
      string selectedPort = comboBoxMonitorComPorts.GetItemText (comboBoxMonitorComPorts.SelectedItem);
      port = new SerialPort (selectedPort, 9600, Parity.None, 8, StopBits.One);
  
      try {
          port.Open ();
  
          while (port.BytesToRead &gt; 0)
              port.ReadByte ();
  
          port.Write ("CPU0CN\n");
  
          Thread.Sleep (3000);
  
          if (port.BytesToRead &gt; 0) {
              String response = port.ReadLine ();
  
              if (response.Equals ("test")) {
                  labelMonitorConnectionStatus.Text = "Connected";
                  btnMonitorConnect.Text = "Disconnect";
                  isConnected = true;
  
                  EnableControls ();
              }
              else {
                  labelMonitorConnectionStatus.Text = "Connection Failed!";
                  btnMonitorConnect.Text = "Connect";
                  isConnected = false;
  
                  port.Close ();
                  ResetControls ();
              }
          }
          else {
              labelMonitorConnectionStatus.Text = "Connection Failed!";
              btnMonitorConnect.Text = "Connect";
              isConnected = false;
  
              port.Close ();
              ResetControls ();
          }
      }
      catch (Exception e) {
          labelMonitorConnectionStatus.Text = "Connection Failed!";
          btnMonitorConnect.Text = "Disconnect";
  
          ResetControls ();
      }
  }
  </code></pre>
  
  <p>That code successfully connects me to the Arduino through a serial com port. Let's focus on this section of the code:</p>
  
  <pre><code>labelMonitorConnectionStatus.Text = "Connecting...";
  
  string selectedPort = comboBoxMonitorComPorts.GetItemText (comboBoxMonitorComPorts.SelectedItem);
  port = new SerialPort (selectedPort, 9600, Parity.None, 8, StopBits.One);
  
  try {
      port.Open ();
  
      while (port.BytesToRead &gt; 0)
          port.ReadByte ();
  
      port.Write ("CPU0CN");
  
      Thread.Sleep (3000);
  </code></pre>
  
  <p><code>labelMonitorConnectionStatus.Text = "Connecting...";</code>
  Set the text on <em>labelMonitorConnectionStatus</em> to "Connecting..."</p>
  
  <p><code>port.Open ();</code>
  Open the port...</p>
  
  <p><code>port.Write ("CPU0CN\n");</code>
  Write communication startup request message through com port</p>
  
  <p><code>Thread.Sleep (3000);</code>
  Wait 3 seconds for response from com port (will reduce this later, of course)</p>
  
  <p>This should change the text to "Connecting...", then wait 3 seconds before being changed to either "Connection Failed!" or "Connected". However, that isn't happening. What's happening instead is the Thread will sleep for 3 seconds—without having set the text to "Connecting..."—then immediately change to either "Connection Failed!" or "Connected". In other words, I never see "Connecting..." on the application. I see "Not Connected" for 3 seconds, then "Connection Failed!" or "Connected".</p>
  
  <p><a href="https://youtu.be/41T0Aw269-A" rel="nofollow noreferrer">Watch this short clip to understand what I see</a></p>
  
  <p>Thanks, in advance, for any help!</p>