Solved

handleMessage trouble/idle state

Posted on 1998-01-22
5
195 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 109
Working with hours 3 65
Error E2158 compiling with Delphi XE10 Seattle 2 115
FMX and jaudiotracker playing memory stream 29 87
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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