0

可能重复:
跨线程操作无效:从线程访问的控件不是在它创建时创建的
线程 跨线程操作无效

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 编程的新手

4

0 回答 0