Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Capture keyboard event within a endless loop.

Posted on 1997-10-05
9
Medium Priority
?
317 Views
Last Modified: 2010-04-04
for example I have a for..next loop. normally I can solve this by using the code below
while true do
begin
     ....
     Application.ProcessMessages;
end;
But using ProcessMessages method will slow down the speed.
How can I capture the keyboard event without using Application.ProcessMessages.
0
Comment
Question by:ccchou
9 Comments
 

Expert Comment

by:Dippen
ID: 1346811
Hi!

Generally, creating endless loops in an event-oriented language is equal to asking for trouble, unless you create a new thread and put the loop there. In my opinion, threads are not very simple to manage and should be avoided if not really needed.
   However, if you write more specifically what your application is supposed to accomplish, I might be able to help you further.

//Dippen
0
 
LVL 1

Expert Comment

by:miv
ID: 1346812
The only thing you can do is to call ProcessMessages only once a second or for every 10.000 loops or so...(depending on the processing taking place in the loop)
0
 
LVL 1

Expert Comment

by:dionysos_swamp
ID: 1346813
one word: Tform.onKeyPress
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ccchou
ID: 1346814
hi..
    I have to say sorry for your answer. Without the Application.ProcessMessages method, any window message can not be received until end of loop. So it can not work.
0
 

Author Comment

by:ccchou
ID: 1346815
My application is real time  graphically display A/D converted data. Speed is critical.
I used Convas for drawing the graph.
More specifically, I want to break the loop when Esc key been pressed.
If I use ProcessMessages in the loop and with  a button.(for say STOP button)
The speed will be only 1/10.

Thanks for everyone who answer my question.
0
 
LVL 1

Expert Comment

by:Gabor
ID: 1346816
I think, you have to use a thread to display the data, and leave the application to run as usual. This solution will be the correct and fastest one.
0
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 200 total points
ID: 1346817
I'd approach this similiar to what Gabor recommended, but with a twist.

First, in the FormActive set a (form) global variable, like bRunDisplay to true.

Second, create a thread that sits in a loop with an Application.ProcessMessages loop looking for the user to press the [Esc] key. Set the thread to a very low priority to allow the data display to have more runtime. Once the thread is created it will run by itself independent of the A/D data display. The only purpose of this thread is to capture the mysterious [Esc] key. If the key is hit, then bRunDisplay is set to FALSE and the thread terminates.

Meanwhile, in a land, far far away the A/D Display is running with something like:

While bRunDisplay = True then
      {Do nifty Graphical stuff here}
else
     {STOP doing nifty Graphical stuff}

The keyboard checker thread and the A/D display are running together and the only thing in common is that they share the bRunDisplay variable which determines if the Display should end.

The only advantage to doing it this way as opposed to just using Application.Processmessages in your main loop is that you are able to set a lower priority for the keyboard scanning loop and it when IT "ProcessesMessages" and the A/D is in a critical need of CPU time, the keyboard scanner will yield (because of it's lower priority) to the main loop.

Sorry for the lengthy spiel and hope this helps :)
Pegasus
0
 

Author Comment

by:ccchou
ID: 1346818
Hi, Pegasus
Thanks for your answer. I think this will work.
But right now this project I am working on is using Delphi 1.0.
Maybe my application must work on Window 3.1.
I'd like to know if you have another method.
Thank you.
0
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1346819
cchou,
Glad to be of help!

The main problem in using Win3.1 with an app like this is that that OS does not support pre-emptive multitasking, a *major* problem for real-time applications! With 3.1 not only do you have to worry about your own application's "timeliness" but also how other CPU processes "hog" the processor, thereby slowing your application.

Unfortunately, without using threads (Win 95/NT, etc) when your application does an Application.ProcessMessages, your application is yielding not only to itself, but also to any other tasks that are running under 3.1

My advice, if you can avoid it, try and use at least Delphi 2.0 and design your application for use with 32 bit O/Ss *only*. Windows 3.1 and Workgroups is slowly going to go away and, unless you have a compelling need, don't expend the effort to accomidate a dying operating system.

Good luck with your project, sounds like fun :)
Pegasus
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

971 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