Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

handleMessage trouble/idle state

Posted on 1998-01-22
5
Medium Priority
?
207 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
  • 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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