Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread Looping and using all CPU

Status
Not open for further replies.

BuilderSpec

Programmer
Dec 24, 2003
383
GB
Hi

New to threads and trying to create a simple Socket server to run as an application. The code i have is a Service Application straight from the File.. New.. Service Application and added a ServerSocket from the Internet tab.

The code I have then is below :

//---------------------------------------------------------------------------
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TService1 *Service1;
//---------------------------------------------------------------------------
__fastcall TService1::TService1(TComponent* Owner)
: TService(Owner)
{

}

TServiceController __fastcall TService1::GetServiceController(void)
{
return (TServiceController) ServiceController;
}

void __stdcall ServiceController(unsigned CtrlCode)
{
Service1->Controller(CtrlCode);
}
//---------------------------------------------------------------------------


void __fastcall TService1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Socket->SendText("ACK");

}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceExecute(TService *Sender)
{
ServerSocket1->Active = true;
while (!Terminated)
{
Service1->ServiceThread->ProcessRequests(true);
}
ServerSocket1->Active = false;


}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceAfterInstall(TService *Sender)
{
DoStart() ;
}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceStart(TService *Sender, bool &Started)
{
ServiceThread->Priority = tpIdle ;
Started = true;
}
//---------------------------------------------------------------------------



What happens is this.. the code runs ok and i can connect to the socket server and send it a message.. it responds with the "ACK" message OK but then the program starts usning 99% CPU permanently.

I am guessing that the

while (!Terminated)
{
Service1->ServiceThread->ProcessRequests(true);
}

is the cause.. any idea what i should put there instead..the help file examples say i should just do what I have done.

Any ideas ?



Hope this helps!

Regards

BuilderSpec
 
Did you get it solved?
I don't know much about how Services work, but you can always use the Sleep()-function to let the CPU rest a little between each loop ;)

Like so: Sleep(100);

Or there is probably some Windows Wait-function that can be used, or use a Timer instead for a loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top