Welcome, Guest
Username: Password: Remember me

TTimer passing a String OnTimeout

Posts 1 to 5 of 5
  • taitai
  • taitai's Avatar
  • OFFLINE
  • Gold User
  • No rank
1 week 5 days ago #11904
Hi. Sorry if this sounds really dumb but I am a C programmer tying to get a handle on this
C++ Builder
I want to create a new TTimer and attach a AnsiString so that when the timeout fires the AnsiString is then used to send to a socket.
I dont want to have a global AnsiString.
I tried creating a class that had the TTimer ,and the AnsiString
but I was unable to read the string when the timeout occured.
whats the best way to do this
Thanks
Log in to reply.
1 week 5 days ago #11910
Yes, you can. This may be simple if you use Firemonkey TTimer components. In this case you have a property TagObject. So you can create an object with an AsnsiString inside.
class MyClass:public TObject {
public:
String str;
}

....
MyClass *strObj = new MyClass;
strObj->str = "Pippo";
Timer1->TagObject = new MyClass;

----
on event timer:
void __fastcall MyForm::Timer1Timer(TObject *Sender) {
TTimer* t = dynamic_cast(Sender);
MyClass *strObj = (MyClass *)t->TagObject;
ShowMessage(strObj->str);
}

....
and TagObject delete automatically the object when Timer is deleted
If you use a VCL TTimer the same thing you can do with property Tag. Pay attention this is only an "int" so is not portable pointer! And also you have to rember to delete object.

Another way is extend TTimer component adding an AnsiString property
Log in to reply.
  • taitai
  • taitai's Avatar
  • OFFLINE
  • Gold User
  • No rank
1 week 4 days ago #11922
Thanks for the reply
unfortunately I am using VCL
how do I go about using the Tag Int to point to the Class Object.
I tried Typecasting but the compiler does not like it
Thanks
Log in to reply.
  • taitai
  • taitai's Avatar
  • OFFLINE
  • Gold User
  • No rank
1 week 4 days ago #11925
Hi

I got this work using the methods below
Would you be so kind to check it over to see if its safe to cast the Object to the Int Tag Property like this
class MyClass:public TObject
{
public:
AnsiString str;
int svc;
};
void __fastcall TScarSatMain::SplitsTimer_Start(AnsiString msg,int svc)
{
Label40->Caption = "";
TTimer *Timer = new TTimer(this);
MyClass *strObj = new MyClass;
strObj->str = msg;
strObj->svc = svc;
Timer->Tag = (int)(strObj);
Timer->Interval = 1000;
Timer->OnTimer = SplitsTimer_Elapsed;
Timer->Enabled = true;
}

void __fastcall TScarSatMain::SplitsTimer_Elapsed(TObject *Sender)
{
TTimer* t = dynamic_cast(Sender);
MyClass *strObj = (MyClass *)t->Tag;
Label40->Caption = strObj->str + IntToStr(strObj->svc);
//ShowMessage(msg);
t->Enabled = false;
delete strObj;
delete Sender;
}

Thanks for you help
Last Edit: 1 week 4 days ago by taitai. Reason: formatting
Log in to reply.
1 week 1 day ago #11973
This is (as suggested) a workaround. Keep in mind this is not portable and the best solution is to extend TTimer object; but this may works.
In general I don't delete objects inside handler (delete Sender). I think is better to delete timer outer from his handler
Log in to reply.
The following user(s) said Thank You: taitai
Moderators: @aprilanne