DoEvents vs. Application.ProcessMessages

Posted on 1998-07-17
Medium Priority
Last Modified: 2010-04-04

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:

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

    Dim StartTime As Single

    StartTime = Timer

    Do While Timer < StartTime + WaitSeconds

End Sub

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

procedure Wait(Msec : longint);
    b : longint;
    b := GetTickCount;
  while (GetTickCount - b) < Msec do

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.


Question by:summerset

Accepted Solution

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;
  b := GetTickCount;
  while (GetTickCount - b) < Msec do begin
    if done then begin
      { release pointer,
        free memory \
        etc then halt}

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


Author Comment

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.



Expert Comment

ID: 1358148
use a timer component

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

587 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