Solved

handleMessage trouble/idle state

Posted on 1998-01-22
5
186 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 50 total points
ID: 1357297
I am happy to cooperate,

Bye
Igor
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

24 Experts available now in Live!

Get 1:1 Help Now