08-17-2010 08:40 AM
Hi all ,
I have a simple question that if in a callback function of a command button, i use a While(bLoop)
loop , where bLoop is a boolean variable initially set to TRUE, then how can i break or exit from this while Loop by using a control on the front panel , which exits the while loop by setting bLoop = FALSE .
I have tried it but it is not exiting
Solved! Go to Solution.
08-17-2010 10:01 AM
The problem here is that CVI normally only executes one callback at a time. While you are in one control callback, no GUI processing and subsequent calls to other callbacks normally occurs.
You can try the following approach - but it is not really the best method:
while (b_loop) { // Inside a callback
... // Perform your tight program loop
ProcessSystemEvents (); // Force CVI to see if any GUI controls need servicing
}
If you have a different control callback to clear the global b_loop, this quick-and-dirty approach might work for you.
You should only use this technique for very simple programs - it can rapidly spiral out of control and end up in a mess if you're not careful. NI recommends that you do not use ProcessSystemEvents() inside a control callback.
JR
08-17-2010 12:42 PM
JR:
NI recommends that you do not use ProcessSystemEvents() inside a control callback? That's surprising! For me, that's one of the main uses of ProcessSystemEvents. You are absolutely right that care needs to be taken. CVI 9 help tells you to take care when using it, and warns that ProcessSystemEvents won't return from a tracking loop for a pull-down menu until the user dismisses the menu, But I haven't seen any warnings not to use it in a control callback. It wouldn't be the first time I missed something (or misused something). Do you have a reference that will put that into perspective?
Thanks
08-18-2010 04:18 AM
Al,
I must confess I can't lay my hands on a definitive statement from NI - perhaps it was a topic in the forum at some stage? Certainly some forum users frown on the idea of calling ProcessSystemEvents() inside a control callback. A programmer needs to be careful with this approach, and novices tend not to be careful, so as far as I'm concerned the general recommendation to beginners stands. (Its a bit like learning to drive: once you have achieved a level of experience you can start to do things your driving instructor would not approve of.)
Having said that, almost all of my programs have a call to ProcessSystemEvents() at the end of a Timer control callback - but here of course there is a significant difference: a Timer callback function is not re-entrant - once entered it cannot be called again until the original callback has completed. This eliminates the possibility of catastrophic recursion which can occur in other (re-entrant) control callbacks, with a carelessly positioned ProcessSystemEvents().
JR