Re: Form OnKeypress event not firing while test loop running



Paul E. Schoen wrote:

I have an application which waits for data to appear on a serial port (which is continuously running), and when it reaches a given threshold it analyzes and stores the data. Once this happens the display shows the running time and current value, and this may be repeated until a certain number of pulses occur, or a preset timeout is reached. At that time, the test is considered complete, and the results can be saved. But it is often annoying to wait until completion of the timeout during which current is off, because the technician knows that the test is complete (or invalid), and the program should be either reset or closed, which saves the data.

The testing environment is such that using a mouse is difficult, so I have implemented an event handler for the form's OnKeypress event, and it works as it should before and after the test. I test for the ESC key, which should close the program, but the event never fires for any key until one of the timeouts occurs. The data analysis is accomplished in a rather large loop which fires on a 200 mSec timer event, and it processes about 480 integer data values (which are sampled at 2400/second). This happens before current has been detected and throughout the duration of the test, and when the test is completed it still processes data but the processing is more limited. The loop also updates several text boxes on the main form.

The program responds to mouse clicks while the test is running and it is possible to close the program by clicking on the Form Close icon. There are other hidden forms that are updated, but the main form remains on top and has focus. CPU usage is about 32%.

Another problem is that there is a RESET button that sometimes seems to not generate its OnClick event, even though the control visually appears to be clicked. I'm running inside the D4 IDE, so it could be related to that.

Well, I tried shutting down Delphi and running just the exe, and I got a stream of error messages about unable to set focus on a hidden control. This happens as soon as the timeout occurs. I need to fix that, and I have an idea where that problem might lie.

I had thought this was going to be a simple fix and I wanted to send the revised program to my customer in the morning, but I'm stumped, and I need sleep. Maybe I'll figure it out tomorrow, but any ideas in the meantime will be appreciated.

Paul


Move your serial port code that is in a tight loop to a secondary thread or, call Application.ProcessMessages in the loop some where, you should employ a keyboard handler in the main form that will lets say set a global variable to some state. ESC key for example, then while your in that loop, you can test for that value incase the user wants to terminate the current function..

P.S.
You should have "Keypreview := True" in the form so that the form
events will see the key strokes first before passing them to the controls. Other wise, You may not see them.



http://webpages.charter.net/jamie_5";

.