Solved

DoEvents vs. Application.ProcessMessages

Posted on 1998-07-17
3
1,747 Views
Last Modified: 2010-04-04
Hi...

I'm a VB programmer who is in the process of learning Delphi 3 (now 4). I have to say -- It's Great! :-)

However, I am trying to rewrite one of my old VB programs in Delphi 3 which I use the following "wait" routine:

''VISUAL BASIC
Public Sub Wait(WaitSeconds As Single)
    'usage: Wait(10) - waits for 10 seconds

    Dim StartTime As Single

    StartTime = Timer

    Do While Timer < StartTime + WaitSeconds
        DoEvents
    Loop

End Sub

In Delphi (this is only one of a couple ways to do it):

//DELPHI:
procedure Wait(Msec : longint);
  var
    b : longint;
  begin
    b := GetTickCount;
  while (GetTickCount - b) < Msec do
    Application.ProcessMessages;
end;


The problem is the difference between DoEvents and Application.ProcessMessages. Using the VB code I can still exit the program by unloading the form, or simply "ending" it. The doevents allows this to happen before the wait time is over. Delphi allows me to minimize, push buttons, etc, but will not execute anything (or let me end the program) until the wait time has ticked out. This has been frustrating.

Is there a different way to interupt the wait time and end the program, etc.??? Any help or sample code would be greatly appreciated.

Sincerely,

-S
0
Comment
Question by:summerset
3 Comments
 
LVL 2

Accepted Solution

by:
kjteng earned 120 total points
ID: 1358146
Are you sure that the program won't let you do anything while you are waiting? I tested your code and found that it does process other messages before timeout EXCEPT that you cannot exit the program.

Why do you want to exit while waiting? What I normally do is tohave some test in the loop to determine whether the program should be terminated. eg

procedure Wait(Msec : longint);
var  b : longint;
begin
  b := GetTickCount;
  while (GetTickCount - b) < Msec do begin
    application.ProcessMessages;
    if done then begin
      { release pointer,
        free memory \
        ...
        etc then halt}
      halt;
    end;
  end;
end;

As you can see from the above code, delphi's approach is safer because the program will not exit unintentionally without releasing windows resources!

0
 

Author Comment

by:summerset
ID: 1358147
I see what you mean. It would be better to throw in a little if or case statement that determined events I would want to end the program??

I've never used Halt...and it stops it dead, but your right...it seems kind of clunky to abruptly end it that way. That would be fine in Win95, but would cause problems in NT. Maybe it's just my approach...afterall, it is an old program that I originally did in VB3. I'm thinking I should create a timer event that just waits 10 seconds and then resumes. If I did that...would I be able to stop the timer in Delphi (without using Halt) and resume something else using Application.ProcessMessages? Or would the timer event have to finish it's 10 seconds before doing something else?

The program loads pictures for part of a presentation. the Wait(time) was the interval between slides. The user  could interupt the interval by pressing a key or clicking the mouse -- and move on to the next slide.

Remember, I'm extremely new to Delphi. ;-) Any help or samples you could give me would be greatly appreciated.

Sincerely,

-S
0
 

Expert Comment

by:bome
ID: 1358148
use a timer component
bome
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

18 Experts available now in Live!

Get 1:1 Help Now