Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Capture keyboard event within a endless loop.

Posted on 1997-10-05
9
Medium Priority
?
313 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
[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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…
Suggested Courses

722 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