LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

run a threaded function on a key-pressed

Hi,
I want to add a feature to my code which run a diffrenet threaded function (which will run in the background) whenever a key is pressed.
The definition of the threaded function should be in the start of the program, and should recognize a key pressed in any state of the application without changing (or adding lines) to internal functions.
 
I've seen some previous posts in this forum, which recommend using Safe-Queues. This option is not good for me since it require (as far as I understood) to add the key pressed to the queue, which means that I have to add lines inside my functions.
 
thanks,
Ido
0 Kudos
Message 1 of 3
(3,219 Views)
Hi Ido,

Take a look at the post here, and see if it's what you are looking for. Hope it helps.

Regards,
Ebele O.
National Instruments
0 Kudos
Message 2 of 3
(3,169 Views)
The scheme described in the other post is only as responsive as the frequency at which system events are processed.

If you have anything happening in any code anywhere in your process that takes a long time, you need to hack in calls to process system events, otherwise the panel callback won't get executed and your keyboard event won't be recognized.

This is unfortunate.  What you really want is some way to absolutely, positively ensure a programatic response to a keyboard event regardless of what your code might be doing at the moment, and you'd like for this to happen without cluttering your code up with lots of ProcessSystemEvents calls.  Perversely, even lots of processsystemevents calls won't help with very situation in which you need the quick response the most - a hardware or software anomaly has hung up your system and your expensive unit under test is burning up.

I've used a separate process, with a single small panel with a single "ABORT" command button that runs at high priority  and simply monitors the ABORT key every hundred milliseconds or so to see if it's been committed.   The OS will ensure the abort process runs regardless of what's happening in the main program as it's higher priority than your main program.  When there's an event, the abort process can do whatever's needed to safe the system.  This could include signalling the main process (but that begs the question of the main process servicing an event related to the signal) or aborting the main process.  Contention for resources between the two processes would have to be provided for (e.g. both processes trying to access a power supply - the main to turn it on, and the abort to turn it off).  If the main process has been aborted this wouldn't be an issue.

Menchar
0 Kudos
Message 3 of 3
(3,149 Views)