1

I have a function that "simulates" chess PGN .txt files. The function looks like this:

PGNFileEinlesen^ pGN = gcnew PGNFileEinlesen(cBoxDateiNamen->Text);
Simulation::DctFigur = dctFigur;
//reset(Reset.Komplett);

// Den Header in die Textbox schreiben
for each(String^ s in pGN->CsvHeader)
{
    rTextBoxZuege->AppendText(s + Environment::NewLine);
}
rTextBoxZuege->AppendText(Environment::NewLine);

// Sleep vor erstem Zug

System::Threading::Thread::Sleep(zeitIntervall);
for (int i = 1; i <= pGN->DtcZuege->Count; i++)
{

    // Falls Siegesbedingung oder Spiel vorzeitit beendet: (*)
    // 1-0 --> weiss gewinnt
    // 0-1 --> schwarz gewinnt
    // 1/2-1/2 --> Remi
    // * --> Partie ist noch nicht beendet   
    addTextRichTextbox(i, pGN->DtcZuege[i]->Split(' ')[0]);

    if (partieBeendet(pGN->DtcZuege[i]->Split(' ')[0]))
    {
        return;
    }
    Simulation::Zug(pGN->DtcZuege[i]->Split(' ')[0], Farbe::Weiss);

    try
    {
        simulationsZug();
    }
    catch (Exception^ e)
    {
        Console::WriteLine("SimulationsZug() " + e->Message);
    }


    System::Threading::Thread::Sleep(zeitIntervall);
    this->farbeAmZug = farbeAmZugAendern();

    addTextRichTextbox(pGN->DtcZuege[i]->Split(' ')[1]);

    if (partieBeendet(pGN->DtcZuege[i]->Split(' ')[1]))
    {
        btnReset->PerformClick();
        return;
    }

    Simulation::Zug(pGN->DtcZuege[i]->Split(' ')[1], Farbe::Schwarz);
    try
    {
        simulationsZug();
    }
    catch (Exception^ e)
    {
        Console::WriteLine("SimulationsZug() " + e->Message);
    }


    System::Threading::Thread::Sleep(zeitIntervall);
    this->farbeAmZug = farbeAmZugAendern();

}

btnReset->Enabled = true;

Now, the problem is, that if I start the simulation, the whole GUI is freezing until the function reaches the end of the for loop.

I tried to create a new thread:

threadSimulation = gcnew Thread(gcnew ThreadStart(this, &Schachfeld::test));

threadSimulation->Start();

but I get the error, that my thread is overlapping with the actual thread because I access controls from the original thread. (and functions too)

Could somebody explain to me what I should do? I didn't find very much by googling.

Thank you!

user3793935
  • 411
  • 5
  • 22
  • 1
    Use Control.BeginInvoke or Control.Invoke methods to access UI from a worker thread. See C++/CLI syntax here: https://stackoverflow.com/questions/20924641/winform-control-begininvoke-invoke-issue – Alex F May 17 '18 at 05:20
  • Thank you, I'll try it later – user3793935 May 17 '18 at 05:35

0 Answers0