DoEvents vs. Application.ProcessMessages

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
summersetAsked:
Who is Participating?
 
kjtengConnect With a Mentor Commented:
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
 
summersetAuthor Commented:
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
 
bomeCommented:
use a timer component
bome
0
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.

All Courses

From novice to tech pro — start learning today.