title: Update GUI from background worker
body: <p>Ok, I know that similar questions were already asked. But here is my thing:</p>
  
  <p>Environment: Visual Studio 2010, C++, Win Forms, Dot Net Framework 4.0, Windows XP SP3.</p>
  
  <p>Everybody says that you should not update the GUI directly from a background task. Well... I am doing it heavily without having the slightest problem. But why? Shouldn't I run into problems, crashes or any other oddities at least from time to time?</p>
  
  <p>My application is a controlling app for projectors. Currently 40,000 lines of code and 7 backgroundworker threads. All backgroundworker threads were created in the Designer. Three of those threads need to do a lot of stuff. Their only output is the user interface, which has a huge set of labels, sliders, numeric input fields, picture boxes and list boxes. The threads use the same functions to update the GUI like the functions that are located directly in the main user interface thread. For the functions it is not clear from where they are called.</p>
  
  <p>I took at first great care with several sub-functions to ensure that the GUI was only updated in the main thread and in the ProgressChanged function. But I am only human and so I lost a bit track of some things and so I accidentally wound up updating the GUI directly from the backgroundworker threads. I never saw any problems and I also got no reports from the users of the software that there are crashes or any other oddities that were not based on simple errors from my side.</p>
  
  <p>So now I did the ultimate test: I added two backgroundworker threads that both rigorously fool around totally randomly with the GUI in an endless loop, both at the same time in order to test if something bad happens. The code makes the buttons flicker, list boxes are filled up and cleared, labels are filled with random text... and so on. Except for that it looks funny it all works perfectly without a single crash or any other strange thing.</p>
  
  <p>So, as I said: It never crashed. It always works flawlessly. What am I missing? What is this "Don't update the GUI from another thread under any circumstances" all about? Is the way how I do it ok or is my computer a miracle PC?</p>
  
  <p>EDIT: What should I expect by breaking the "Don't update" law? Crashes, because the garbage collection might move the UI around in the memory and the threads create memory problems? Or just that I don't know which of the two or three (or 7) threads won and was overwriting the data from the other thread?</p>
  
  <p>Here is a part of my test code with the two threads:</p>
  
  <pre><code>bool rndBool() {
      return (rand()%10 &gt;= 5 ? false : true);
  }
  
  
  // Test Thread 1
  private:
      System::Void backgroundWorkerTest1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
          BackgroundWorker^ workerTest1 = dynamic_cast&lt;BackgroundWorker^&gt;(sender);
  
          int i = 0;
          while ( true ) { // Loop forever
              if ( workerTest1-&gt;CancellationPending ) { e-&gt;Cancel = true; return; } // Cancel Thread
  
              int sleeptime = 1;
  
              doubleInput_CubeX-&gt;Value = (int)rndDouble(50, 1000);
              doubleInput_CubeY-&gt;Value = (int)rndDouble(50, 1000);
              Sleep(sleeptime); labelStatus-&gt;Text = String::Format("(Test1) {0}", i); i++;
              Sleep(sleeptime); button_PowerOn-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_PowerStandby-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_PowerOn-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_PowerStandby-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_SaveAll-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_SaveAll-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_ReadCurrent-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_ReadCurrent-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_MappingCroppingOff-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_GeometryFactoryReset-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_LoadAdjustment-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_LoadFramework-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_ColorFactoryReset-&gt;Enabled = rndBool();
              Sleep(sleeptime); colorPickerButton_ServiceColor-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_ServiceColorOff-&gt;Enabled = rndBool();
              this-&gt;Refresh();
          }
      }
  
  // Test Thread 2
  private:
      System::Void backgroundWorkerTest2_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
          BackgroundWorker^ workerTest2 = dynamic_cast&lt;BackgroundWorker^&gt;(sender);
  
          int i = 0;
          while ( true ) { // Loop forever
              if ( workerTest2-&gt;CancellationPending ) { e-&gt;Cancel = true; return; } // Cancel Thread
  
              int sleeptime = 3;
  
              doubleInput_CubeX-&gt;Value = (int)rndDouble(i, 50, 1000);
              doubleInput_CubeY-&gt;Value = (int)rndDouble(i, 50, 1000);
              Sleep(sleeptime); labelStatus-&gt;Text = String::Format("(Test2) {0}", i); i++;
              Sleep(sleeptime); button_PowerOn-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_PowerStandby-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_PowerOn-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_PowerStandby-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_SaveAll-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_SaveAll-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_ReadCurrent-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonItem_ReadCurrent-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_MappingCroppingOff-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_GeometryFactoryReset-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_LoadAdjustment-&gt;Enabled = rndBool();
              Sleep(sleeptime); button_LoadFramework-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_ColorFactoryReset-&gt;Enabled = rndBool();
              Sleep(sleeptime); colorPickerButton_ServiceColor-&gt;Enabled = rndBool();
              Sleep(sleeptime); buttonX_ServiceColorOff-&gt;Enabled = rndBool();
              this-&gt;Refresh();
          }
      }
  </code></pre>