Board index » cppbuilder » TTimer and it's onTimer event

TTimer and it's onTimer event

Hi,
I have a global variable  'timerElapsed' of type boolean. I want to
communicate with the serial port using Tcomport component. I send some data
and should receive data or wait untill the timer expires for reception. I
have used TTimer to indicate when the timeout occurs. But the program
control does not go to the Ontimer event function at all . I have a function
Refresh() which i enter first and i am struck up in the while loop. The
condition which can break this loop is in the ontimer event. The control
does not go to ontimer event until the while loop is terminated. Please help
me.
I have never used threads.

Siraj

The code is as follows:

void TForm1::Refresh()

{
      if (Comport1->Connected)
   Comport1->Close();
  else
  Comport1->Open( );

   wStr="S058 20.0\n";
   Comport1->WriteStr(wStr);
   timerElapsed=false;
  Timer->Interval=100;
  Timer->Enabled=true;

   While (buffer = = 0 && timerElapsed= =false)
   {
   buffer = Comport1->ReadStr(rStr,Comport1->InputCount() );    //assume
buffer value is always 0
   }

   if (timerElapsed= = true)
   Timer->Enabled=false;

Quote
}

void_fastcall TForm1::TimerTimer(Tobject *Sender)
{
    timerElapsed=true;
    Application->MessageBox("time out",NULL,MB_OK);
Quote
}

 

Re:TTimer and it's onTimer event


Your while() loop isn't allowing the application to process any messages.
TTimer uses a WM_TIMER message to trigger it's OnTimer event.  You need to
call Application->ProcessMessages() inside your loop.

Gambit

Quote
"siraj" <siraj...@omnisens.ch> wrote in message news:3c7c076f_1@dnews...
> I have used TTimer to indicate when the timeout occurs. But the program
> control does not go to the Ontimer event function at all . I have a
function
> Refresh() which i enter first and i am struck up in the while loop.

Re:TTimer and it's onTimer event


Thank you vey much for your help. i used Application->ProcessMessages()
inside the loop and it worked.
I have one more problem. how to process only one message in the message
queue and return back the control.
I have two timers timer1 and timer2. Timer1 starts as soon as the
application starts. Timer2 starts after i press a button, the routine of
which i have written in my previous post on this topic.

Help me please, Thanking you in advance

Siraj

"Remy Lebeau [TeamB]" <gambi...@gte.net> a crit dans le message news:
3c7c11be$1_2@dnews...

Quote
> Your while() loop isn't allowing the application to process any messages.
> TTimer uses a WM_TIMER message to trigger it's OnTimer event.  You need to
> call Application->ProcessMessages() inside your loop.

> Gambit

> "siraj" <siraj...@omnisens.ch> wrote in message news:3c7c076f_1@dnews...

> > I have used TTimer to indicate when the timeout occurs. But the program
> > control does not go to the Ontimer event function at all . I have a
> function
> > Refresh() which i enter first and i am struck up in the while loop.

Re:TTimer and it's onTimer event


Why would you want to do that?  A lot of controls depend on that message
queue.  Using Application->ProcessMessages() allows all controls to process
their pending messages in one pass.

If you must only handle 1 message at a time, then you can call
Application->HandleMessage() instead.

Gambit

Quote
"siraj" <siraj...@omnisens.ch> wrote in message news:3c7cac25_1@dnews...
> I have one more problem. how to process only one message in the message
> queue and return back the control.

Other Threads