Board index » cppbuilder » how to write receiving thread correctly (Indy)

how to write receiving thread correctly (Indy)


2006-03-24 03:21:26 PM
cppbuilder52
Hi All,
I am writing a network application using Indy (BCB 6). I want to
transfer messages of the type {int size; ....; char *data_of_this_size}.
The receiving thread looks like this:
while(!Terminated && Client->Connected())
{
Client->ReadBuffer(block->Header, 22);
if(block->Decode(block->Header, 22)) // convert received buffer
to e.g. size
{
Client->ReadBuffer(block->Buffer, block->Size);
...;
}
}
the problem is that this code is obviously block the thread when I want
to shutdown application
Is there better way to write the receiving thread ?
Boris
 
 

Re:how to write receiving thread correctly (Indy)

"Boris Epel" < XXXX@XXXXX.COM >wrote in message
Quote
the problem is that this code is obviously block the
thread when I want to shutdown application
Simply disconnect the socket from the main thread. Any futher socket
operations on the socket will cause the reading code to throw an exception,
which will then break your while() loop so the thread can terminate.
Also, your thread code should be checking the Terminated property before
performing any lengthy operations, and before calling Synchronize() (if you
use that). Also, the main thread should call the thread's Terminate()
method before closing the socket, and then call the thread's WaitFor()
method to wait for the thread to fully terminate itself.
Gambit