Solved

DoEvents vs. Application.ProcessMessages

Posted on 1998-07-17
3
1,814 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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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