Avoiding "Application.ProcessMessages"

0 views
Skip to first unread message

Lutz Kutscher

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
Hi,

I'm writing a procedure, which may take a long time to execute. I want
the user to be able to stop it at any point of execution.
I know, I can use Application.ProcessMessages to handle a click of the
"STOP" button. I also thougt about creating a new thread for the
procedure.
In either case I have to check for a variable "ProcedureStopped" (with
Application.ProcessMessages) or I have to check for MyThread.Terminated.

Both of these require frequent polling. I'd prefer some kind of
interrupt to exit the procedure.
Is there any way to tell Windows to "interrupt enable"?
I remember this from programming some old 8-bit micro controllers and I
thought something as sophisticated <g> as Windows NT can't have thrown
such a powerful feature over board ?

Any help is appreciated

Lutz


Greg Lorriman

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to

> In either case I have to check for a variable "ProcedureStopped" (with
> Application.ProcessMessages) or I have to check for MyThread.Terminated.
>
> Both of these require frequent polling. I'd prefer some kind of
> interrupt to exit the procedure.

Are you thinking that polling is nasty and heavy? In this case it's light
and quick. Even talking to "terminated" ain't too bad (afaik). In any case
it depends on how you arrange your loops. If you are checking on every
interation then it may be that you are checking too often.


> Is there any way to tell Windows to "interrupt enable"?
> I remember this from programming some old 8-bit micro controllers and I
> thought something as sophisticated <g> as Windows NT can't have thrown
> such a powerful feature over board ?

I suspect you would need to write a device driver for that.

However you could invoke a windows timer....perhaps. If you've got the
source it may be as simple as lifting it from TTimer and puttng it into your
thread.


--
Greg Lorriman
Handy, free utils at http://www.lorriman.demon.co.uk


Hallvard Vassbotn

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
Lutz Kutscher wrote in message <380EC621...@Pattburg-Poetzsch.de>...

>Both of these require frequent polling. I'd prefer some kind of
>interrupt to exit the procedure.

Unfortunately, by default the VCL does not have a very
thread-friendly mainthread message loop. It is possible
to override the message loop by hooking Application.OnIdle
and call MsgWaitForMultipleObjects there. Doing this you can
build your own multithread aware message loop and thus easiliy
get notified of events and other signals raised from secondary
threads.

I wrote an article about this in The Delphi Magazine, issue 40.
Even if you don't subscribe, you can download the code at
http://www.itecuk.com/delmag/dmdisk.htm

/Hallvard Vassbotn


Peter Below (TeamB)

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
> I'm writing a procedure, which may take a long time to execute. I want
> the user to be able to stop it at any point of execution.
> I know, I can use Application.ProcessMessages to handle a click of the
> "STOP" button. I also thougt about creating a new thread for the
> procedure.
> In either case I have to check for a variable "ProcedureStopped" (with
> Application.ProcessMessages) or I have to check for MyThread.Terminated.
>
> Both of these require frequent polling. I'd prefer some kind of
> interrupt to exit the procedure.
> Is there any way to tell Windows to "interrupt enable"?

No. Even interupts cannot blandly terminate a running code stream, they
can set a flag somewhere the code can check to exit in an orderly fashion
and that is the same you do with Application.ProcessMessages. The latter
is fairly time-consuiming so should not be called on each round of a tight
processing loop, for example. But checking on Terminated in a thread is a
cheap operation since has no extra code baggage attached.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


Yoram Halberstam

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
With the thread mechanism you can assign the OnTerminated event to check
when the process is terminated.

Dr. Delphi

Lutz Kutscher wrote:

> Hi,


>
> I'm writing a procedure, which may take a long time to execute. I want
> the user to be able to stop it at any point of execution.
> I know, I can use Application.ProcessMessages to handle a click of the
> "STOP" button. I also thougt about creating a new thread for the
> procedure.
> In either case I have to check for a variable "ProcedureStopped" (with
> Application.ProcessMessages) or I have to check for MyThread.Terminated.
>
> Both of these require frequent polling. I'd prefer some kind of
> interrupt to exit the procedure.
> Is there any way to tell Windows to "interrupt enable"?

> I remember this from programming some old 8-bit micro controllers and I
> thought something as sophisticated <g> as Windows NT can't have thrown
> such a powerful feature over board ?
>

Lutz Kutscher

unread,
Oct 22, 1999, 3:00:00 AM10/22/99
to

Hi,

thanks for all your help.
Peter, of course you're right - obviously I didn't bring my thoughts to an
end.

Lutz


Reply all
Reply to author
Forward
0 new messages