Hello.
Using firemonkey 11. 1 c++ builder for android platform, I want that when a button is clicked, a function (void Loop()) is executed, in which two components are hidden (C1 and C2 visible=false), a cycle of 1,000,000 intervals, a ProgressBar (PB) componenet is executed in the THREAD while the loop is active; and this will be repeated until the loop ends or the Timage image (Imge1) is clicked.
At the end of the loop, the progress bar will be hidden and components C1 and C2 will be displayed. I want to know if it is possible to control the progress bar in the thread and how to do it.
I have tried it in the following way.:
THREAD:
ifndef ThreadUnitH
#define ThreadUnitH
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
class TProgressThread : public TThread
{
private:
TProgressBar *ProgressBar;
bool &StopFlag;
protected:
void __fastcall Execute();
public:
__fastcall TProgressThread(TProgressBar *PB, bool &stopFlag);
};
#endif
//---------------------------------------------------------------------------
.CPP
//---------------------------------------------------------------------------
#include <fmx.h>
#include "PB.h"
__fastcall TProgressThread::TProgressThread(TProgressBar *PB, bool &stopFlag)
: TThread(true), ProgressBar(PB), StopFlag(stopFlag)
{
FreeOnTerminate = true;
}
void __fastcall TProgressThread::Execute()
{
while (!StopFlag)
{
TThread::Synchronize(nullptr, [this]()
{
if (ProgressBar->Value < ProgressBar->Max)
ProgressBar->Value += 100;
});
Sleep(10);
}
}
FORM:
.H
//---------------------------------------------------------------------------
class TF : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TImage *Image1;
TProgressBar *PB;
TLabel *C1;
TLabel *C2;
TLabel *Label1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Image1Click(TObject *Sender);
private: // User declarations
bool StopThread;
public: // User declarations
void filtrar();
__fastcall TF(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TF *F;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
.CPP
//---------------------------------------------------------------------------
#include <fmx.h>
#include <System.Threading.hpp> // Para usar TTask
#pragma hdrstop
#pragma hdrstop
#include "Progress.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TF *F;
TThread *workerThread = nullptr;
//---------------------------------------------------------------------------
__fastcall TF::TF(TComponent* Owner)
: TForm(Owner)
{
StopThread = false;
}
//---------------------------------------------------------------------------
void __fastcall TF::Button1Click(TObject *Sender)
{
C1->Visible = false;
C2->Visible = false;
PB->Visible = true;
PB->Value = 0;
PB->Max = 1000000;
TProgressThread *Thread = new TProgressThread(PB, StopThread);
Thread->Start();
Loop();
}
void TF::Loop()
{
int i=0;
for (i = 0; i < 1000000; i++)
{
if (StopThread) {Break;}
Sleep(10);
PB->Visible = false;
TThread::Synchronize(nullptr, [this]()
{
C1->Visible = true;
C2->Visible = true;
});
}
//---------------------------------------------------------------------------
void __fastcall TF::Image1Click(TObject *Sender)
{
StopThread = true;
}
Attached image for easy understanding
Any ideas
Thank you