LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Suspend Timer callbacks

I have a multithreading application, where one thread is running cyclic (by a timer callback), and the second is an asynchronous thread, called by a user interface. Both threads use the same resource, the serial port.

To avoid both threads using the serial port at the same time I am using threadlocks.

My problem is now as following: The asynchronous task often runs longer than the period of the cyclic task. The cyclic task stops until the thread locks indicate a free serial port. But now the cyclic task is called as often as it would have been called during waiting for the serial port.

I would like stopping the cyclic thread, waiting for the free port and afterwards going on without multiple calls.

Any ideas?

Michael
0 Kudos
Message 1 of 2
(3,067 Views)
Yes I have had the need to turn off timer callbacks several times myself. From what you mentioned I assumed that you would like for the timer callback to be called every X seconds if possible, but if the comport is busy to either drop cycles or prolong the current one. You should be able to do this by having the follwing as the first thing in your timer callback.

int CVICALLBACK Timer_Callback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
SetCtrlAttribute (panel, PAN_TIMER, ATTR_ENABLED, 0);
CmtGetLock (com_lock);
ResetTimer (panel, PAN_TIMER);
SetCtrlAttribute (panel, PAN_TIMER, ATTR_ENABLED, 1);
/*... rest of callback ...*/
return 0;
}

Reseting the timer is optional, it jus
t makes the next event happen X seconds after rigth now, as opposed to whenever the next period would have occured.

I also know that you can choose to allow events to be processed while you are waiting in CmtGetLock. If you do this then you could also take an approch of allowing the timer to continue, but bailing out of the callback if another callback is in progress:

int CVICALLBACK Timer_Callback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
static int busy = 0;
if (busy) return 0;
busy = 1;
CmtGetLock (com_lock);
/*... rest of callback*/
busy = 0;
return 0;
}

I hope that one of these solves your problem.
jackson
0 Kudos
Message 2 of 2
(3,067 Views)