Solved

handleMessage trouble/idle state

Posted on 1998-01-22
5
183 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
Comment Utility
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
Comment Utility
I use a TTimer.

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

0
 
LVL 5

Expert Comment

by:inter
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
I am happy to cooperate,

Bye
Igor
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now