title: Invoke from outside of a class .NET
body: <p>i am usinng Visual Studio C++ 2010.</p>
  
  <p>I want to make thread-safe change in GUI from other than main thread, from function declared outside of a main Form class. Here is some code i have:</p>
  
  <p><strong>Outside of a main class:</strong></p>
  
  <pre><code>public delegate void DEBUGDelegate(String^ text);
  </code></pre>
  
  <p>(...)</p>
  
  <pre><code>int lua_debug(lua_State *L){
      // boolean debug(message)
      Globals^ Global = gcnew Globals;
      String^ debugMsg = gcnew String(lua_tostring(L, 1));
  
      DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global-&gt;FORM, &amp;Form1::DEBUGDelegateMethod);
      Global-&gt;FORM-&gt;Invoke(myDelegate, gcnew array&lt;Object^&gt; { "HEYO! \r\n" });
  
      lua_pushboolean(L, true);
      return 1;
  }
  </code></pre>
  
  <p><strong>Inside of a main class:</strong></p>
  
  <pre><code>public ref class Form1 : public System::Windows::Forms::Form
  {
  
  (...)
  
      public: void DEBUGDelegateMethod(String^ text)
      {
              this-&gt;DEBUGBOX-&gt;Text += text;
      }
  
  (...)
  
      private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
      {
          Globals^ Global = gcnew Globals;
          Global-&gt;FORM = this;
      }
  
      private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e)
      {
          Globals^ Global = gcnew Globals;
          DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(this, &amp;Form1::DEBUGDelegateMethod);
          this-&gt;Invoke(myDelegate, gcnew array&lt;Object^&gt; { "HEYO! \r\n" });
      }
  }
  </code></pre>
  
  <p><strong>So the problem is that if i comment function "lua_debug" and the rest stays untoutched, it works fine and clicking on button1 makes text appears on debug textbox. When i uncomment the part with lua_debug, there is an error:</strong></p>
  
  <pre><code>d:\prog\c++\x\x\Form1.h(146): error C2653: 'Form1' : is not a class or namespace name
  1&gt;d:\prog\c++\x\x\Form1.h(146): error C2065: 'DEBUGDelegateMethod' : undeclared identifier
  1&gt;d:\prog\c++\x\x\Form1.h(146): error C3350: 'X::DEBUGDelegate' : a delegate constructor expects 2 argument(s)
  </code></pre>
  
  <p>146 line is:</p>
  
  <pre><code>DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global-&gt;FORM, &amp;Form1::DEBUGDelegateMethod);
  </code></pre>
  
  <p><strong>===================================================
  @EDIT</strong></p>
  
  <p>After moving lua_debug after Form1 declaration, i get this error:</p>
  
  <pre><code>d:\prog\c++\x\x\Form1.h(1829): error C2440: 'initializing' : cannot convert from    'System::Windows::Forms::Form ^' to 'X::Form1 ^'
  1&gt;          No user-defined-conversion operator available, or
  1&gt;          Cast from base to derived requires safe_cast or static_cast
  1&gt;d:\prog\c++\x\x\Form1.h(1829): error C3754: delegate constructor: member function    'X::Form1::DEBUGDelegateMethod' cannot be called on an instance of type 'System::Windows::Forms::Form ^'
  </code></pre>
  
  <p>in line:</p>
  
  <pre><code>DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global-&gt;FORM, &amp;Form1::DEBUGDelegateMethod);
  </code></pre>
  
  <p>Global->FORM is declared as:</p>
  
  <pre><code>static Form^ FORM;
  </code></pre>
  
  <p>in Globals class.</p>