Forum > Beginners

Program loop "runaway" while waiting on keyboard inputs

(1/3) > >>

new2thisagain395:
I'm working on a small application that has a main program loop, that monitors the value stored in a string buffer and does things based on the values of what happens to be in that buffer at any given time.

The application uses the OnKeyPress event handler which monitors keypresses and stores either a single character, a string, or a password in the buffer (depending on what the user is doing).

The problem is, the main loop seems to get "hung up" and doesn't even get the application's window to the point where it's visible.  I have to kill it using the System Monitor (on ubuntu).

The main loop is essentially:

--- Code: ---while (1) do begin
   if (buffer = (something I care about) do_something
   if (buffer = (something else that I care about) do_something_else
   etc.
   etc.
end;

--- End code ---

If I remove the while loop, the stuff inside it works fine, although (as expected) only one time.

What is wrong in my thinking here?  Thanks.

Thaddy:
The while loop needs an application.processmessages inside, because otherwise the loop keeps running without communication with the rest of the application. In a console/terminal app I would use a thread in its simplest form. I can give you an example for that. Not now, but soon. I have to check wether peekmessage/dispatch message etc. are supported for Linux as well. On windows this is easy. On linux I expect that it is enough to include baseunix and call pause() in the loop. Pause will wait for an input signal before it continues and that includes keyboard signals of course. Oh, and I think a semaphore can be a similar solution with sem_wait in the loop. At home I can write examples much easier, now I am doing my Easter shopping  ;D

Handoko:
Thaddy is right, what new2thisagain395 needs is an Application.ProcessMessages.

I am not good in explaining it but here I provide some examples:
https://forum.lazarus.freepascal.org/index.php/topic,65319.msg497638.html#msg497638
https://forum.lazarus.freepascal.org/index.php/topic,44416.msg312238.html#msg312238
https://forum.lazarus.freepascal.org/index.php/topic,43806.msg307101.html#msg307101

Download the demos in the links above, run them and run them again but after removing the line "Application.ProcessMessages;". Then you should know the differences and understand when to use it.

new2thisagain395:
Thanks.

I tried including application.processmessages in the loop where I check for keystrokes or strings in the buffer (the pseudocode at the top of this thread), but it made no difference.  That's on a linux machine though, and I've read that application.processmessages doesn't work the same or at all on linux (unless I read wrong).

Probably spinning off a separate thread to handle just the keyboard events is the better way to go. 

Thaddy:
No. You read wrong indeed (or read the wrong message and believed it). It works a bit different but should do the same, at least in a gui application. For a terminal application I will add an example later this weekend if you can not figure out my tips.

Navigation

[0] Message Index

[#] Next page

Go to full version