?
Solved

DoEvents vs. Application.ProcessMessages

Posted on 1998-07-17
3
Medium Priority
?
1,893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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