0

Android FMX C++ builder : Why does my thread seem to stop until I click a control

I'm using C++ Builder 10.4 update 2 to write an android app. A thread, "FindThread", has been created to scan an array of words for words that start with a particular string, and put these words into a TListBox. WORDCT is the number of words in the array (currently 65197)

When I run the app, enter something in BeginEdit, and hit the "Go" button, only a certain number of items are displayed before the thread seems to hang (and this number varies each time I run the app), but If I even click a button on the form without an OnClick event, the thread seems to spring back to life for a few seconds.

I've got the code to show where it is every 200 words. This is how I can see that the thread seems to stop.

// FindThreadUnit.cpp :
void __fastcall TFindThread::Execute()
{
    if (Terminated)
        return;

    UnicodeString start = MainForm->BeginEdit->Text.UpperCase();
    UnicodeString u;

    for (Index=0; Index<WORDCT; Index++)
    {
        if (Terminated)
            break;
        u = Words[Index];
        Include = false;
        if (u.SubString(0,start.Length()).CompareIC(start)==0)
            Include = true;
        Synchronize(Sync);
    }
}

void __fastcall TFindThread::Sync(void)
{
    if (Include)
    {
        MainForm->ResultsListBox->Items->Add(Words[Index]);
        Include = false;
    }
    if ((Index%100)==0)
        MainForm->HeaderLabel->Text = Words[Index];
}

// FindThreadUnit.h :

class TFindThread : public TThread
{
protected:
    void __fastcall Execute(); // Perform the search
public:
    __fastcall TFindThread(bool CreateSuspended);
    void __fastcall Sync(void);
    bool Include;   // used in the synchronised Sync() function
    int Index;      // index into the Words[] array
};

// MainUnit.cpp :

void __fastcall TMainForm::GoSpeedButtonClick(TObject *Sender)
{
    if (GoSpeedButton->Text.CompareIC(L"Go")==0)
    {
        GoSpeedButton->Text = L"Stop";
        ResultsListBox->Items->Clear();

        FindThread = new TFindThread(false); // don't CreateSuspended
    }
    else
    if (GoSpeedButton->Text.CompareIC(L"Stop")==0)
    {
        FindThread->Terminate();
        GoSpeedButton->Text = L"Go";
    }
}
Nigel Stevens
  • 147
  • 1
  • 1
  • 9
  • You can't access UI controls from outside of the UI thread. Reading from `MainForm->BeginEdit->Text` must be in a `Synchronize()`'d call. – Remy Lebeau Sep 05 '22 at 04:12

0 Answers0