Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

handleMessage trouble/idle state

Posted on 1998-01-22
5
Medium Priority
?
203 Views
Last Modified: 2012-05-04
           Hi!

I have written an application which creates a few threads which each create one timer object and then wait for the timer messages to activate them and do their stuff and so on.

So basically the execute procedure looks like this:

procedure TMyThread.Execute;
begin
     While Not Terminated Do
     Begin
//case 1) Application.HandleMessage;
//case 2) Application.ProcessMessages;
//case 3)
     End;
end;

1) The Application goes in the idle state, responds to TTimer events, but does not return to the Execute function any more, therefore not checking the Terminated property any more and therefore never terminating!

2) The application repaints the windows and responds to termination requests, but I noticed it consumes all CPU resources (100%) which, of course, is unnacceptable.

3) The application not only consumes all CPU resources, but also doesn't respond to paint messages well.

What exactly does "idle state" mean? Does it mean that the the system suspends the application (or the thread) until a message is received? What would be a harmless message to send?

Do these functions affect Threads or the entire Application?

Juris.
0
Comment
Question by:juris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1357293
Dear Juris,

Do you use TTimer for timing or you create a multimedia timer? Please explain the details of your code a bit more!

About the CPU resources:
- Try to insert Sleep(10) for example in your threads loop, this solves it (or timing is that much critical we should use another approach)
About ProcessMessages called from thread:
- This is not required and when you call it, it transfers the control to WIN32 and returns when message queue becomes empty, so this may cause lock up because WM_TIMER is sent applications message queue periodically!

Igor

0
 

Author Comment

by:juris
ID: 1357294
I use a TTimer.

Yeah, sleep() really helped I think. Thanks.

0
 
LVL 5

Expert Comment

by:inter
ID: 1357295
Sorry, but I still can not understand the mechanism. Please briefly list the step of creating thread, where to create timer, where to suspend thread etc.

Igor
0
 

Author Comment

by:juris
ID: 1357296
Hey, inter.

Thanks a lot for your help.

Could you post an answer so I can give you credit?

My code was really lame because I missed that sleep(). Now it works OK.

FYI, now it looks like this:

procedure TMyThread.Execute;
Var
   LastTime, CurTime : TDateTime;
begin
     Launch;
     LastTime := Time;
     While Not Terminated Do
     Begin
          Sleep(100);
          CurTime := Time;
          If (CurTime > StartTime) And (CurTime < EndTime) And (LastTime + Interval <= CurTime) Then
          Begin
               LastTime := CurTime;
               DoStuff;
          End;
     End;
end;

0
 
LVL 5

Accepted Solution

by:
inter earned 200 total points
ID: 1357297
I am happy to cooperate,

Bye
Igor
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question