LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

my problem about loop, i cannot stop the loop!

pls see the attachment! i cann't stop the loop. pls help me?
0 Kudos
Message 1 of 6
(3,305 Views)
You have three main problems.
1. You while statement is wrong. In C, a single = is an assignment. == is an equivalence test. Your statement while (v=1) always assigns a value of 1 to v. That is an endless loop. while (v==1) will run as long as v equals 1.
2. v is not initialized. After you correct the while to while (v==1), the first time into the loop, v does not have an assigned value. You could set v=1 prior to the loop or you could use a do while loop (which tests the condition at the end of each loop iteration) instead of a while loop (which tests the condition at the start of each loop iteration).
3. Your loop is before the RunUserInterface statement, so CVI is not running the user interface, so it doesn't see you trying to operate the switch to stop the lo
op. You could do something like the following:
3.1 Make the On-Off switch default to Off.
3.2 Create a callback function for the On-Off switch. (In the UI Editor, double-clock on the On-Off switch to edit the control, enter a callback function name, click OK, then right-click on the control and select Generate Control Callback).
3.3 Move your loop to create your random numbers to the EVENT_COMMIT case in the callback. Like you tried to do initially, stop the loop when On-Off goes False.
0 Kudos
Message 2 of 6
(3,305 Views)
You can set the CVI compiler to detect errors like while (v=1). In the project window (not a code window), goto Options >> Build Options, then check Detect assignments in conditional expressions, then click OK. The next time you compile, CVI will give you a warning.
0 Kudos
Message 3 of 6
(3,305 Views)
While you're in a callback like one that I suggest in step 3 of my earlier answer, CVI won't automatically respond to events like mouse clicks. If you want to use a button to stop the loop, you need to call ProcessSystemEvents() in your loop. See the CVI help for ProcessSystemEvents().
0 Kudos
Message 4 of 6
(3,305 Views)
Dear AI S!Thanks a lot for your help! I have solved all the question you entioned. Now there comes another question. Please see my modified cvi project in the attachment. I find that only the On/Off switch's state is "OFF" can i run "Quit" to quit the UIR. If I placed the swith's state to be "ON", i cannot run "Quit" to quit the UIR directly.
0 Kudos
Message 5 of 6
(3,305 Views)
When the switch is On, you are in a loop in BinaryCallback. When you press Quit with the switch On, QuitUserInterface gets called but the user interface won't quit until you exit BinaryCallback.
One easy solution, since you already have v as a global, is to add one line to QuitCallback() in the EVENT_COMMIT case. Add
v=0;
right before QuitUserInterface(). That will stop the loop in BinaryCallback as soon as QuitCallback returns, so BinaryCallback will end and QuitUserInterface will quit the user interface and the program will end.
P.S. Have you thought of using meaningfull variable names to make your code more self-documenting? For example, instead of v, call it runGenNumLoop.
P.P.S. The ZIP format is more popular on this forum than the
RAR format. More people may be able to see your code if you ZIP it. Also you don't need to include the .cdb and .exe files in your posts.
0 Kudos
Message 6 of 6
(3,305 Views)