Темы в Строитель 6

Привет у меня есть проблема, это остановило мой прогресс

Мне нужно, чтобы начать 200 секунд до 0 (обратный отсчет в заголовке), нажав на кнопку, у меня 48 кнопок, которые являются таймерами
На Google я нашел некоторые темы, но не знаю, как его использовать

timer_01.cpp:

__fastcall timer_01::timer_01(bool CreateSuspended)
: TThread(CreateSuspended)
{
}

void __fastcall timer_01::Execute(TButton* buton)
{
if(buton->Caption=="Flash"){
for(int i=flash_time;i>0;i--){
buton->Caption=i;
Sleep(1000);
};
}
}
//---------------------------------------------------------------------------

и моя кнопка в main_program.cpp

    void __fastcall TForm1::Button4Click(TObject *Sender)
{
Execute(Button4);

}

и включить в main_program.cpp

#include "timer_01.h"

мой конструктор main_program.h я добавил:

 void __fastcall Execute(TButton* buton);

0

Решение

Ваш код — абсолютно неправильный способ использования TThread, Попробуйте что-то более похожее на это:

timer_01.h:

class timer_01 : public TThread
{
private:
TButton *fButton;
String fValue;
String __fastcall GetButtonCaption();
void __fastcall DoGetButtonCaption();
void __fastcall SetButtonCaption(const String &AValue);
void __fastcall DoSetButtonCaption();
protected:
void __fastcall Execute();
public:
__fastcall timer_01(TButton *AButton);
};

timer_01.cpp:

__fastcall timer_01::timer_01(TButton *AButton)
: TThread(true), fButton(AButton)
{
FreeOnTerminate = true;
}

void __fastcall timer_01::Execute()
{
if (GetButtonCaption() == "Flash")
{
for(int i = flash_time; (i > 0) && (!Terminated); --i)
{
SetButtonCaption(i);
if (!Terminated)
Sleep(1000);
}
}
}

String __fastcall timer_01::GetButtonCaption()
{
Synchronize(&DoGetButtonCaption);
return fValue;
}

void __fastcall timer_01::DoGetButtonCaption()
{
fValue = fButton->Caption;
}

void __fastcall timer_01::SetButtonCaption(const String &AValue)
{
fValue = AValue;
Synchronize(&DoSetButtonCaption);
}

void __fastcall timer_01::DoSetButtonCaption()
{
fButton->Caption = fValue;
}

main_program.cpp

#include "timer_01.h"
timer_01 *timer = NULL;

void __fastcall TForm1::Button4Click(TObject *Sender)
{
if (timer)
{
timer->Terminate();
do
{
CheckSynchronize();
Sleep(10);
}
while (timer);
}

timer = new timer_01(Button4);
timer->OnTerminate = &TimerTerminated;
timer->Resume();
}

void __fastcall TForm1::TimerTerminated(TObject *Sender)
{
timer = NULL;
}

С учетом сказанного вам на самом деле не нужно TThread за такой простой таймер. TTimer будет работать так же хорошо:

void __fastcall TForm1::Button4Click(TObject *Sender)
{
Timer1->Interval = 1000;
Timer1->Tag = flash_time;
Timer1->Enabled = true;
}

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if (Timer1->Tag > 0)
{
Button4->Caption = Timer1->Tag;
Timer1->Tag = Timer1->Tag - 1;
}
else
Timer1->Enabled = false;
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector