LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to abort a callback function while it's running in CVI?

Solved!
Go to solution

Hi, Guys,

I'm a LabVIEW user, and am intesrting in CVI recently.

See my question as attached please. It's so easy in LabVIEW, but it annoys me for several days.

 

Num would increase 1 and delay 1 second per iteration once you click button Run.
My question is,
Is it possible to abort the Run_callback function by clicking button Stop while the loop is running ?
If Yes, could you please help modify my example.

 

Thanks.

 

Download All
0 Kudos
Message 1 of 5
(4,061 Views)
Solution
Accepted by topic author labc

Hello - 

 

To answer your question indirectly, perhaps a better way to achieve what you are looking for is through the use of a timer.  You could drop a timer control on your UIR and set its interval to be 1 second.  This timer control will call a routine in your code at the interval you define.  Then, you just enable or disable the timer from the callbacks for the run button and stop button.  I've modified your example and attached it below.  Let us know if you have any questions!

 

NickB

National Instruments

0 Kudos
Message 2 of 5
(4,056 Views)

If you want to use LabView as the model, the loop in LabView checks the Stop button every time through the loop.  With a couple of lines of code, you can do the same thing in CVI.

1. Don't use a Command Button for the Stop button, use a Toggle Button.

2. In CVI, once a callback is entered, user interface buttons are ignored until you return from the callback, unless you call ProcessSystemEvents().  See the CVI help for ProcessSystemEvents.  So in your loop, you need to call ProcessSystemEvents().

3. Read the Stop button value using GetCtrlVal in the loop in the callback for the Run button.  Stop does not need its own callback.

 

See the attached example, in which I modified your example to do the three steps above.

 

I also added a panel callback so the close button (x in the upper right-hand corner of the window) is active.  Even for an example program, it's convenient to be able to close the program from the user interface instead of going back to the CVI environment to abort it.

0 Kudos
Message 3 of 5
(4,038 Views)

labc:

 

I answered your question on face value: how do you exit a callback?

 

There are many examples where you may need to interrupt a callback.  But to solve your example problem (starting and stopping an incrementing value), NickB's redesign of using a timer is much better.  Most real programs do something more than just increment a value.  Letting a timer take care of that is much better than doing it in a loop with delays.  It frees up the CPU to do other tasks, and it lets the rest of your program continue to be event driven.

 

CVI is natively event driven.  Creating an endless loop where you need to poll user interface controls defeats much of the power of the event driven model.

 

So there are various ways you could interrupt a callback, but try to design your programs to reduce the cases where you need to, and rely more on the event-driven model of CVI.

 

If you want to see another example of a timer being used to update a variable, look at this thread.

http://forums.ni.com/t5/LabWindows-CVI/changing-value-of-variable-periodically/m-p/1243526#M48908

0 Kudos
Message 4 of 5
(4,021 Views)

Hi, AI S,nickb,
Thank you! Your ideas truly expand my mind. I fix it up with ProcessSystemEvents() just now by myself.
Now, that's no need to post it for your review.

 

 

 

0 Kudos
Message 5 of 5
(3,982 Views)