little john
February 8th, 2012, 11:15 PM
Hello All,
First thing I have to say is that I am do not know much about implementing multiple threads in an application.
Having said that here is my problem:
I am using RAD Studio C++ builder 2010 and I need to have multiple forms of the same type open, these forms need to poll a device @ a set interval via an ip address using a TidTCPClient, I have tried a number of methods but none so far have been reliable enough.
my Execute method has
std::auto_ptr<TTimer> t(new TTimer(NULL));
t->Interval = 100;
t->OnTimer = &tTimer;
t->Enabled = true;
std::auto_ptr<TTimer> tRead(new TTimer(NULL));
tRead->Interval = interval;
tRead->OnTimer = &tReadTimer;
tRead->Enabled = true;
while(!Terminated)
{
if(terminate)
{
t->Enabled = false;
tRead->Enabled = false;
}
if (MsgWaitForMultipleObjects(0, NULL, FALSE, 800, QS_ALLINPUT) == WAIT_OBJECT_0)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
my timer event has
if(Terminated)
{
terminate = true;
return;
}
ConnectTCPClient();
UnicodeString s;
values = NULL;
if(!offLine)
{
try
{
try
{
tcpWorker->IOHandler->WriteLn(device->requestString); //send the request
values = tcpWorker->IOHandler->ReadLn();
//should end request go here
if(!tcpWorker->IOHandler->ReadLnTimedout && read)
{
tcpWorker->IOHandler->WriteLn(device->endRequest); //send the end data request
Synchronize(&updateParent);
read = false;
}
else
offLine = true;
}
catch(EIdConnClosedGracefully & cg)
{
//tcpWorker->Disconnect();
}
catch(EIdException & eid)
{
s = eid.Message;
}
}
__finally
{
if(tcpWorker->Connected())
tcpWorker->Disconnect();
}
}
each form creates it's own thread and I think that it is here that the problem begins, I am not sure if I should have one thread or one for each running form
the above code seems to work to a point but tends to lock up randomly, this may be due to connection timeout with th TidTCPClient or something else.
I have tried to use a TCriticalSection and its Acquire() and Release() methods but did not help me.
When I tried using a single thread, I checked for the signalled state of the owner form but this caused more problems.
I have seached High and Low for real world examples but found nothing for my needs so am asking if anyone can help out here.
Cheers
First thing I have to say is that I am do not know much about implementing multiple threads in an application.
Having said that here is my problem:
I am using RAD Studio C++ builder 2010 and I need to have multiple forms of the same type open, these forms need to poll a device @ a set interval via an ip address using a TidTCPClient, I have tried a number of methods but none so far have been reliable enough.
my Execute method has
std::auto_ptr<TTimer> t(new TTimer(NULL));
t->Interval = 100;
t->OnTimer = &tTimer;
t->Enabled = true;
std::auto_ptr<TTimer> tRead(new TTimer(NULL));
tRead->Interval = interval;
tRead->OnTimer = &tReadTimer;
tRead->Enabled = true;
while(!Terminated)
{
if(terminate)
{
t->Enabled = false;
tRead->Enabled = false;
}
if (MsgWaitForMultipleObjects(0, NULL, FALSE, 800, QS_ALLINPUT) == WAIT_OBJECT_0)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
my timer event has
if(Terminated)
{
terminate = true;
return;
}
ConnectTCPClient();
UnicodeString s;
values = NULL;
if(!offLine)
{
try
{
try
{
tcpWorker->IOHandler->WriteLn(device->requestString); //send the request
values = tcpWorker->IOHandler->ReadLn();
//should end request go here
if(!tcpWorker->IOHandler->ReadLnTimedout && read)
{
tcpWorker->IOHandler->WriteLn(device->endRequest); //send the end data request
Synchronize(&updateParent);
read = false;
}
else
offLine = true;
}
catch(EIdConnClosedGracefully & cg)
{
//tcpWorker->Disconnect();
}
catch(EIdException & eid)
{
s = eid.Message;
}
}
__finally
{
if(tcpWorker->Connected())
tcpWorker->Disconnect();
}
}
each form creates it's own thread and I think that it is here that the problem begins, I am not sure if I should have one thread or one for each running form
the above code seems to work to a point but tends to lock up randomly, this may be due to connection timeout with th TidTCPClient or something else.
I have tried to use a TCriticalSection and its Acquire() and Release() methods but did not help me.
When I tried using a single thread, I checked for the signalled state of the owner form but this caused more problems.
I have seached High and Low for real world examples but found nothing for my needs so am asking if anyone can help out here.
Cheers