title: C++/CLI Serial COM DataReceived handled as trigger switch gives error
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the">Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on</a><br>
    <a href="https://stackoverflow.com/questions/5037470/cross-thread-operation-not-valid">Cross-thread operation not valid</a>  </p>
  </blockquote>
  
  
  
  <p><strong>fmain.h</strong> Code (partial)</p>
  
  <pre><code>    #define LEFT 37
      #define UP 38
      #define RIGHT 39
      #define DOWN 40
       ...
              // 
              // tabControl1
              // 
              this-&gt;tabControl1-&gt;Controls-&gt;Add(this-&gt;tabPage1);
              this-&gt;tabControl1-&gt;Controls-&gt;Add(this-&gt;tabPage2);
              this-&gt;tabControl1-&gt;Controls-&gt;Add(this-&gt;tabPage3);
              this-&gt;tabControl1-&gt;Location = System::Drawing::Point(2, -1);
              this-&gt;tabControl1-&gt;Name = L"tabControl1";
              this-&gt;tabControl1-&gt;SelectedIndex = 0;
              this-&gt;tabControl1-&gt;Size = System::Drawing::Size(298, 255);
              this-&gt;tabControl1-&gt;SizeMode = System::Windows::Forms::TabSizeMode::Fixed;
              this-&gt;tabControl1-&gt;TabIndex = 2;
              this-&gt;tabControl1-&gt;KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &amp;fMain::tabControl1_KeyDown);
  
          ...
      //keyboard handle in TabControl (not worked in Form)
      private: System::Void tabControl1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
                      if ((btnShuffle-&gt;Enabled) | (PictureLoaded == false))
                          return;
  
                      switch(e-&gt;KeyCode)
                      {
                      case LEFT:
                          if (blankTile.Col &gt; 0)
                              MoveTile(blankTile.Col - 1, blankTile.Row);
                          break;
                      case UP:
                          if (blankTile.Row &gt; 0)
                              MoveTile(blankTile.Col, blankTile.Row - 1);
                          break;
                      case RIGHT:
                          if (blankTile.Col &lt; nCols - 1)
                              MoveTile(blankTile.Col + 1, blankTile.Row);
                          break;
                      case DOWN:
                          if (blankTile.Row &lt; nRows - 1)
                              MoveTile(blankTile.Col, blankTile.Row + 1);
                          break;
                      }
  
                      e-&gt;Handled = true;
  
                      CheckFinish();
                   }
  
          ...
  
      //Serial communication data received
      private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
                       RxData = serialPort1-&gt;ReadExisting();
  
                       switch(System::Convert::ToInt32(RxData))
                       {
                       case LEFT:
                           if (blankTile.Col &gt; 0)
                               MoveTile(blankTile.Col - 1, blankTile.Row);
                           break;
                       case UP:
                           if (blankTile.Row &gt; 0)
                               MoveTile(blankTile.Col, blankTile.Row - 1);
                           break;
                       case RIGHT:
                           if (blankTile.Col &lt; nCols - 1)
                               MoveTile(blankTile.Col + 1, blankTile.Row);
                           break;
                       case DOWN:
                           if (blankTile.Row &lt; nRows - 1)
                               MoveTile(blankTile.Col, blankTile.Row + 1);
                           break;
                       }
                   }
  
      ...
  </code></pre>
  
  <p>giving me error:
  Cross-thread operation not valid: Control 'fTile' accessed from a thread other than the thread it was created on.</p>
  
  <p>i hope someone can help me.. 
  ps:im still newbie in programming c++/cli</p>