Board index » cppbuilder » TTimer problems

TTimer problems


2004-01-08 12:43:03 AM
cppbuilder59
I have a TCP server app and I want the connections to timeout after a while.
There are a few classes involved in the process as shown below. The problem
is the timer event never gets triggered. Any ideas why?
Thanks in advance
Miguel
//main.h
class C
{
private:
void __fastcall timeoutTimerEvent(TObject *Sender);
public:
__fastcall C(TIdPeerThread *thread);
}
//main.cpp
void __fastcall A::TCPServerConnect(TIdPeerThread *AThread)
{
b->createC(AThread);
}
void __fastcall B::createC(TIdPeerThread *thread)
{
C *newC = new C(thread);
}
__fastcall C::C(TIdPeerThread *thread)
{
timeoutTimer = new TTimer(NULL);
timeoutTimer->OnTimer = timeoutTimerEvent;
timeoutTimer->Interval = CONNECTION_TIMEOUT;
timeoutTimer->Enabled = true;
}
void __fastcall C::timeoutTimerEvent(TObject *Sender)
{
disconnect();
}
 
 

Re:TTimer problems

"Miguel Q Moraes" < XXXX@XXXXX.COM >wrote in message
Quote
I have a TCP server app and I want the connections to
timeout after a while.
Indy already has native timeouts implementated. Why are you trying to
implement them manually? Each connection has a ReadTmeout property. When
the server's OnExecute event is triggered, you can try reading the socket.
If the reading times out, it will throw an exception. You can either catch
that exception and disconnect the client manually, or you can simply allow
the exception to escapse back into the server code, which will then
disconnect the client for you.
Quote
There are a few classes involved in the process as shown below. The
problem is the timer event never gets triggered. Any ideas why?
Because TTimer is a message-based component, it relies on the processing of
WM_TIMER messages. However, you are creating the TTimer instances in the
context of worker threads that have no message queues at all, thus no way
for TTimer to receive and handle WM_TIMER messages (or any other messages,
for that matter).
If you must use a timer in this design, then look at using a threaded timer
instead. Either use a Win32 API Multimedia timer via timeSetEvent(), or
simply spawn your own worker thread that uses Sleep() or
WaitForSingleObject() for its timings.
Gambit