From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly exit ChangeDetection thread?

Solved!
Go to solution

I call the ChangeDetection feature of the NI USB-6525 with the following C code in a C++ program:

 

DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateDIChan(taskHandle,"Dev1/port1/line0","",DAQmx_Val_ChanPerLine));
DAQmxErrChk (DAQmxCfgChangeDetectionTiming(taskHandle,"Dev1/port1/line0","Dev1/port1/line0",DAQmx_Val_FiniteSamps,1));
DAQmxErrChk (DAQmxRegisterSignalEvent(taskHandle,DAQmx_Val_ChangeDetectionEvent,0,ChangeDetectionCallback,NULL));
DAQmxErrChk (DAQmxGetTaskNumChans(taskHandle,&numLines));

DAQmxErrChk (DAQmxStartTask(taskHandle));

As far as I understand, this creates a separate thread that sets the USB-6525 into ChangeDetection mode. When the device detects a signal change, the C++ program calls a function (called ChangeDetectionCallback() in this case), which runs in its own thread (or perhaps the same thread, but in any case not the thread used to call the above code). That's explained here and is due to the "0" argument in DAQmxRegisterSignalEvent().

 

What I'd like to know is: while this thread is running (either the one created by one of the above functions or the one that ChangeDetectionCallback() runs in), is it ok to exit the program? Could there potentially be any problems resulting from unfreed resources, for example? Are there any steps I should take to ensure everything exits properly, like making sure to always call DAQmxStopTask() and DAQmxClearTask() before exiting the program?

0 Kudos
Message 1 of 5
(5,974 Views)
Solution
Accepted by topic author brinmr

Hi,

 

if you click the exit button, you have to free all the resources and threads which have been used within your program.

You mentioned the right functions, if you are working with DAQmx, you have to stop and clear the Task. If you are working with threads (Thread Safe queue for example) you have to free them, too. If you opened any files for reading /writing, you should close them to give the resources and the memory free again.


@brinmr wrote:

I call the ChangeDetection feature of the NI USB-6525 with the following C code in a C++ program:

 

DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateDIChan(taskHandle,"Dev1/port1/line0","",DAQmx_Val_ChanPerLine));
DAQmxErrChk (DAQmxCfgChangeDetectionTiming(taskHandle,"Dev1/port1/line0","Dev1/port1/line0",DAQmx_Val_FiniteSamps,1));
DAQmxErrChk (DAQmxRegisterSignalEvent(taskHandle,DAQmx_Val_ChangeDetectionEvent,0,ChangeDetectionCallback,NULL));
DAQmxErrChk (DAQmxGetTaskNumChans(taskHandle,&numLines));

DAQmxErrChk (DAQmxStartTask(taskHandle));

As far as I understand, this creates a separate thread that sets the USB-6525 into ChangeDetection mode. When the device detects a signal change, the C++ program calls a function (called ChangeDetectionCallback() in this case), which runs in its own thread (or perhaps the same thread, but in any case not the thread used to call the above code). That's explained here and is due to the "0" argument in DAQmxRegisterSignalEvent().

 

What I'd like to know is: while this thread is running (either the one created by one of the above functions or the one that ChangeDetectionCallback() runs in), is it ok to exit the program? Could there potentially be any problems resulting from unfreed resources, for example? Are there any steps I should take to ensure everything exits properly, like making sure to always call DAQmxStopTask() and DAQmxClearTask() before exiting the program?


 

 

Best Regards, Fabian

Message 2 of 5
(5,894 Views)

Hi,

 

I appreciate the response. I will be sure to stop and clear the task before exiting.

 

Regarding threads, I'm pretty sure DAQmxStartTask() creates a new thread (let's call it the task thread) because it returns immediately and allows the calling thread to continue while the ChangeDetection task is running simultaneously. However, I don't know how to close the task thread because I can't seem to get its handle. Does either DAQmxStopTask() or DAQmxClearTask() free the task thread? I don't see any information about threads in the documentation of these functions.

 

(By the way, I'm not familiar with Thread Safe because I'm using the ANSI C functions, not CVI.)

 

Best regards,

brinmr

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

Hi,

 

you are right, DAQmxStop only stops the Task (but the task still exist, you could start it again), DAQmxClear clears the Task and gives all resources free. DAQmxClearTask also clears the thread.

 

Best Regards, Fabian 

Message 4 of 5
(5,881 Views)

Now I also see that DAQmxCreateTask(), not DAQmxStartTask(), creates the task thread.

0 Kudos
Message 5 of 5
(5,865 Views)