title: C++ CLI Invoke issues
body: <p>I have a MainForm class (as you'd expect, it is a form) that has a text box on it. I also have another class called 'Application_Server' That does a load of other stuff (not just form-background related, quite a lot of network based stuff etc.).</p>
  
  <p>The Application_Server class runs in it's own thread, but needs to be able to update the controls on the form, for this question, we will stick with just the textbox.</p>
  
  <p>The problem is that even though I am executing the command to set the text of the textBox control via 'Invoke' I am still getting the following exception during runtime:</p>
  
  <blockquote>
    <p>Additional information: Cross-thread operation not valid: Control
    'DebugTextBox' accessed from a thread other than the thread it was
    created on.</p>
  </blockquote>
  
  <p>What could be causing this? I am definitely invoking a delegate within MainForm.</p>
  
  <p>Here are the relevant code segments (cut down for readability):</p>
  
  <p>MainForm.h:</p>
  
  <pre><code>public ref class MainForm : public System::Windows::Forms::Form {
  
          delegate void del_updateDebugText(String^ msg);
          del_updateDebugText^ updateDebugText = gcnew del_updateDebugText(this, &amp;MainForm::postDebugMessage);
  
  private: void postDebugMessage(String^ message);
  };
  </code></pre>
  
  <p>MainForm.cpp:</p>
  
  <pre><code>void EagleEye_Server::MainForm::postDebugMessage(String^ message)
  {
      Monitor::Enter(DebugTextBox);
      if (this-&gt;DebugTextBox-&gt;InvokeRequired)
      {
          this-&gt;Invoke(updateDebugText, gcnew array&lt;Object^&gt; { message });
      }
      else
      {
          this-&gt;DebugTextBox-&gt;AppendText(message);
      }
      Monitor::Exit(DebugTextBox);
  }
  </code></pre>
  
  <p>And finally, the code calling it:</p>
  
  <pre><code>void ServerAppManager::postDebugMessage(System::String^ message)
  {
      mainFormHandle-&gt;updateDebugText(message);
  }
  
  void ServerAppManager::applicationStep()
  {
      postDebugMessage("Starting\n");
      // This is Run in seperate thread in MainForm.cpp
      while (s_appState == ApplicationState::RUN)
      {       
          postDebugMessage("Testing\n");      
      }
  
  }
  </code></pre>
  
  <p>Thanks!</p>