LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

SetAsyncTimerAttribute

I can't seem to be able to find any help file explaining the attributes in SetAsyncTimerAttribute.

I would like to know what does enabled really means. Correct me if i'm wrong.
I gather that 0 means to disable the timer and 1 means to enabled the timer. But what does it really disable? does it stop the timer from counting? or does it still count but do not make it as an event timer tick.

i wrote something like this using the enabled but it does not seem to be correct. After continue is pressed, the next time popup appears is not the correct duration. Sometimes for this case, is quite close to 10 sec but sometimes popup appears after 4 sec. i do not understand why.

void CVICALLBACK Execute (int menuBar, int menuItem, void *callbackData, int panel)
{
TimerUp = 0;

TimerHandle = NewAsyncTimer (10.0, -1, 1, ExecutionTimer, NULL);

Execution (); // Go to function

DiscardAsyncTimer (-1);
}

int CVICALLBACK ExecutionTimer (int reserved, int theTimerId, int event, void *callbackData,
int eventData1, int eventData2)
{
if (event == EVENT_TIMER_TICK)
{
SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 0); // disable timer
TimerUp = 1;
}
return 0;
}

void Execution (void)
{
while (some other parameters)
{
if (TimerUp == 0)
do some stuff

else
{
popup ... contine?

if (yes)
{
TimerUp = 0;
SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 1); // enable timer
}

else
return;
}
}
}


Second thing, i read somewhere in the help file "A negative value will make the timer generate events until it is discarded by a call to DiscardAsyncTimer or suspended by a call to SuspendAsyncTimer." But i can't seem to find "SuspendAsyncTimer" function. Is it available?

Lastly, "A call to create or discard an asynchronous timer will not complete (will block) until all outstanding asynchronous callbacks return." Does this mean that i cannot discard the timer inside the event timer tick?

thanks for the help....

cheers
AL
0 Kudos
Message 1 of 10
(5,481 Views)
Hello,

I do not know which version of CVI you are using, but in V7.1 the help is very clear.

- Enabling the timer means:

Set ASYNC_ATTR_ENABLED to 0 to disable the timer and set ASYNC_ATTR_ENABLED to 1 to enable the timer. Setting ASYNC_ATTR_ENABLED to 0 and then back to 1 does not disturb the ongoing interval schedule. If one or more callbacks are missed while ASYNC_ATTR_ENABLE is 0, they are lost.

- Suspending the timer:

The function you are searching is SuspendAsyncTimerCallbacks.

Michael
0 Kudos
Message 2 of 10
(5,465 Views)
Hi Michael,

Thanks for replying. I'm using CVI Version 7.0. The help just says that there are such attributes but no further explainations were given. hmm, some of the improvements in CVI 7.1... From experience and trial testing, i guessed that 1 is to enable and 0 means to disable, but apparently it was not working correctly so i asked for help.

What i need is this, in summary:

Execution Starts
Timer Starts

When 10 Seconds is up, stop timer, pause execution
Popup appears asking "To continue or not?" (it does not matter how long user takes to decide to continue or not..)

If Continue, restart timer at 10 seconds, continue with execution and repeat popup after 10 seconds

Else, terminate execution.



I have tried both functions

SuspendAsyncTimerCallbacks ();
SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 0);

and after testing, both seems the same to me. Correct me if i'm wrong. Both apparently just disabled the callback but the countdown stills goes on.

After the 10 seconds, popup appears and if i take 6 seconds to decide to continue, 4 seconds later the popup appears again. (which is not what i want!) and this happens for both the functions. So is there any difference between the two functions??

Is there any solution for me to restart it at 10 seconds after user decides to continue?

i have thought of discarding the timer after 10 seconds and create a new one again if user continues but is there a better way? i thought there should be something to stop or pause the counting.

regards
AL
0 Kudos
Message 3 of 10
(5,451 Views)
Hi AL.

You are correct. The following calls do not change the callback schedule, they merely prevent the callbacks occurring:
SuspendAsyncTimerCallbacks ();
SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 0);

You wrote:
"i have thought of discarding the timer after 10 seconds and create a new one again if user continues ..."

This is in fact the way to go.

When you create an asynchronous timer, you specify how / when it is to be discarded.
In this case, let's allow it to generate just _one_ callback.

Try this:
/*****************************************************/
void CVICALLBACK Execute (int menuBar, int menuItem, void *callbackData, int panel)
{
TimerUp = 0;

// Note: second parameter (Count) changed from -1 to 1
// So, the timer will be automatically discarded after 1 callback
TimerHandle = NewAsyncTimer (10.0, 1, 1, ExecutionTimer, NULL);

Execution (); // Go to function

// Realise that this discards ALL async timers; maybe this belongs in main()?
//DiscardAsyncTimer (-1);
}

int CVICALLBACK ExecutionTimer (int reserved, int theTimerId, int event, void *callbackData,
int eventData1, int eventData2)
{
if (event == EVENT_TIMER_TICK)
{
// Don't need this line, as timer will be automatically discarded
//SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 0); // disable timer
TimerUp = 1;
}
return 0;
}

void Execution (void)
{
while (some other parameters)
{
if (TimerUp == 0)
do some stuff

else
{
popup ... continue?

if (yes)
{
TimerUp = 0;
// Don't need this line, as the timer no longer exists!
//SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 1); // enable timer

// Create a new timer and continue
// This guarantees the corrrect timing for your callback
TimerHandle = NewAsyncTimer (10.0, 1, 1, ExecutionTimer, NULL);
}

else
return;
}
}
}
/*****************************************************/

By the way, you can get the help you wanted by right-clicking (on a control or the body of the function panel), by pressing [F1] or [Shift-F1], or via the Help menu.

Good luck.
Colin.
Message 4 of 10
(5,430 Views)
Hi Colin,

Thanks a million for your help. It works exactly the way i want it to!!! 😃

I just want to confirm if there was any other way. Didn't realise the count was so useful in that sense!!! I would have written something to call a function to discard the timer using "DiscardAsyncTimer (-1);"

So these two functions actually does the same thing?
SuspendAsyncTimerCallbacks ();
SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 0);

I do read the help and search through the forums to find what i need, just that i feel that the help file sometimes is not clearly explained. There seems to be an improvement in CVI V7.1 though!!

Once again, thanks for the help!! Greatly appreciate it.

Cheers
AL
0 Kudos
Message 5 of 10
(5,410 Views)
Hi again AL.

You're welcome 🙂

> So these two functions actually does the same thing?
> SuspendAsyncTimerCallbacks ();
> SetAsyncTimerAttribute (TimerHandle, ASYNC_ATTR_ENABLED, 0);

Not exactly. SuspendAsyncTimerCallbacks affects ALL async timers in your app; the other call affects just the one.

Cheers,
Colin.
0 Kudos
Message 6 of 10
(5,396 Views)
Hi Colin,

thanks for replying, you have been one great help!!

Cheers
AL
0 Kudos
Message 7 of 10
(5,388 Views)
Hi Colin,

i have another question. Originally, we discard the timer immediately after one callback. But let's say if my execution finishes before the callback occurs, then the timer is not discarded correct? and this will pose problems to the program?

cheers
AL

Message Edited by AL on 03-22-2005 12:14 AM

0 Kudos
Message 8 of 10
(5,346 Views)
Hi again AL.

From the Help for DiscardAsyncTimer():
"A call to create or discard an asynchronous timer will not complete (will block) until all outstanding asynchronous callbacks return."
This would apply whether or not the timer is to be automatically discarded.

To be safe, I suggest that you call
DiscardAsyncTimer(-1);
at the bottom of main(). It may be worthwhile to first display a message to let the user know what is happening, if your timer interval is more than a few seconds.

Colin.
0 Kudos
Message 9 of 10
(5,320 Views)
Hello Colin,

I want to join your discussion, as the topic is interesting for me too.

I have a similar problem as AL. A Timer is running, and for some reasons the timer function can last longer than the timer intervall lasts. In such cases I want to resume the call of the timer.

I tried to do this with

SetAsyncTimerAttribute (timerId, ASYNC_ATTR_ENABLED, 0);

but I obtain a very strange behaviour. With only this line in my timer callback function I disable timer callbacks. But if I enable the timer at the end of the callback function, the "missing" callbacks are performed.

I enclose a short demo for this behaviour and would be glad if you have a look at this.

Michael
0 Kudos
Message 10 of 10
(5,312 Views)