LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

DiscardAsyncTimer

I have this bit of code in my quit callback:

int CVICALLBACK QuitCallback (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT:
            
            DiscardAsyncTimer (-1);
            while(timer_on==1);  //just to make sure!?!?
            //Quit daq photodiode function
            keep_reading=0;
            //Turn off daq tasks
            DAQmxWriteDigitalLines(taskHandle,1,1,10.0,DAQmx_Val_GroupByChannel,binary_data_off,NULL,NULL);
            DAQmxClearTask (taskHandle);
            DAQmxClearTask (read_dig_taskHandle);

            viClose(defaultRM);    
            DiscardPanel (mainHandle);
            QuitUserInterface (0);
            break;
        }
    return 0;
}

In my timer function I access the serial port and access the mainHandle etc.  On rare occasions when it goes to this function to quit my program, the program is unable to quit properly.  It seems the timer function still performs tasks after the discard timer is called.  I can verify this by putting a breakpoint at the ViClose or DiscardPanel lines and then seeing the step through code go to my timer function and perform the code with errors (since I gotten rid of the handles necessary to perform the timer.

Note this is from the NI help file:

"Note  A call to create or discard an asynchronous timer will not complete (will block) until all outstanding asynchronous callbacks return."

Thanks in advance


0 Kudos
Message 1 of 5
(3,800 Views)
eward,
    I have not worked much at all using async timers, but I thought I would throw out a couple ideas and someone else can always point you in a better direction if need be.
1) in the quit_callback, you might first want to set the async_attr_enabled to false so that it will quit being called and then discard the timer further down.
2) more what I was thinking.... if your code is quitting, is there really a purpose served by discarding the timer?  Couldn't you just eliminate the call to discard and not lose any functionality?
 
Scott M.
0 Kudos
Message 2 of 5
(3,790 Views)
Two points:

-Setting the async_attr_enabled to false before discarding the timer doesnt help. 
-I also have a check to see if the DiscardAsyncTimer function returns any error (<0) and this doesnt seem to have any problems. 

All I want to do is close the whole program properly.  But I'm assuming I have to close some of the things i open (ie the resource manager) and not just call QuitUserInterface.  But if this is the case I am still curious as to how/why the timer is being called.  It seems the documentation is pretty clear that the timer should be done and complete.  The DiscardAsyncTimer has given me problems in the past and am trying to understand it as well.

Thanks


0 Kudos
Message 3 of 5
(3,772 Views)
Hi eward,

Just for clarification, what do you mean when you say your program does not quit properly (i.e. error messages, hanging, crashing, etc.)? Are you creating more than one timer in your application? If not, have you tried passing the discard function the actual timer ID instead of -1? The function will only block until callbacks for the specific timer ID you provide are done.  I would think that -1 would block for all instances but it's possible that's not the case.  I would need to test it out to be sure. 

Have a great day,

Ecleamus R.
National Instruments
Applications Engineer
0 Kudos
Message 4 of 5
(3,744 Views)
Hi Eward.

Are you handling EVENT_DISCARD in your async timer callback function? If not, you could add:
case EVENT_DISCARD:
to your switch statement, and handle clean-up operations under this case, finally setting your timer_on variable to 0.

Keep in mind:
    - If you call PostDeferredCall() in the timer callback, the deferred callback will not execute until QuitCallback() has returned. This may cause unexpected behaviour.
    - Placing breakpoints in QuitCallback() may create a backlog of timer callbacks.
If you still have problems, it would be useful for us to see your timer callback code.

Regards,
Colin.
0 Kudos
Message 5 of 5
(3,737 Views)