可能重复:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
Cross-thread operation not valid
fmain.h 代码(部分)
#define LEFT 37
#define UP 38
#define RIGHT 39
#define DOWN 40
...
//
// tabControl1
//
this->tabControl1->Controls->Add(this->tabPage1);
this->tabControl1->Controls->Add(this->tabPage2);
this->tabControl1->Controls->Add(this->tabPage3);
this->tabControl1->Location = System::Drawing::Point(2, -1);
this->tabControl1->Name = L"tabControl1";
this->tabControl1->SelectedIndex = 0;
this->tabControl1->Size = System::Drawing::Size(298, 255);
this->tabControl1->SizeMode = System::Windows::Forms::TabSizeMode::Fixed;
this->tabControl1->TabIndex = 2;
this->tabControl1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &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->Enabled) | (PictureLoaded == false))
return;
switch(e->KeyCode)
{
case LEFT:
if (blankTile.Col > 0)
MoveTile(blankTile.Col - 1, blankTile.Row);
break;
case UP:
if (blankTile.Row > 0)
MoveTile(blankTile.Col, blankTile.Row - 1);
break;
case RIGHT:
if (blankTile.Col < nCols - 1)
MoveTile(blankTile.Col + 1, blankTile.Row);
break;
case DOWN:
if (blankTile.Row < nRows - 1)
MoveTile(blankTile.Col, blankTile.Row + 1);
break;
}
e->Handled = true;
CheckFinish();
}
...
//Serial communication data received
private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
RxData = serialPort1->ReadExisting();
switch(System::Convert::ToInt32(RxData))
{
case LEFT:
if (blankTile.Col > 0)
MoveTile(blankTile.Col - 1, blankTile.Row);
break;
case UP:
if (blankTile.Row > 0)
MoveTile(blankTile.Col, blankTile.Row - 1);
break;
case RIGHT:
if (blankTile.Col < nCols - 1)
MoveTile(blankTile.Col + 1, blankTile.Row);
break;
case DOWN:
if (blankTile.Row < nRows - 1)
MoveTile(blankTile.Col, blankTile.Row + 1);
break;
}
}
...
给我错误: 跨线程操作无效:控制'fTile'从其创建的线程以外的线程访问。
我希望有人可以帮助我.. ps:我还是编程c ++ / cli的新手