Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

AI Task stop problem

My program has a small bug that causes it to stop responding. When the form loads I create a AItask which starts on a digitial trigger. I have a start button that starts the AItask. I also have a stop buttun that stops and reset the AItask. If I try to stop the task before it has received the trigger, the program stops responding and I have to use the task manager to close the program. I'm using c#. Can anyone help me with this problem. Thanks

JMD
0 Kudos
Message 1 of 6
(5,095 Views)
The Stop method waits for any pending operations to complete before stopping the task, so it is likely that you have an asynchronous read operation that hasn't finished at the time you call Stop. In this case, the Stop call is merely waiting for the read to time out and exit gracefully before stopping the task.

If you need to force the task to stop immediately and abort the pending read, you can call Task.Control(TaskAction.Abort) and this will cause the read to exit (with an exception) immediately.

If this doesn't solve your problem, please let us know.

Tony H.
Measurement Studio
Message 2 of 6
(5,095 Views)
Thanks Tony H.
Task.Control(TaskAction.Abort) solved the issue. I have another question about the exception that is thrown and what processes are running.

When you set up a AItask that starts on a digital trigger. After you start the task but before the trigger is recieved is the asynchronus read called? Is there documentation about when exceptions are thrown? Thanks

JMD
0 Kudos
Message 3 of 6
(5,095 Views)
The read is something you would have had to call. Either you called a BeginRead method somewhere, starting an asynchronous read, or you called a Read method from another thread than you are attempting to call Stop from.

If you look for the "NI-DAQmx Driver Error Codes" topic in your online help, you can see a long list of conditions that will cause the NI-DAQmx driver to throw an exception. In general, NI-DAQmx will throw an exception when it cannot complete an operation you initiated. For instance, if you call Read it will throw an exception if the read times out and it cannot complete, if the task is aborted, if the task is acquiring data too fast and your read buffer overflowed, or possibly other types of error conditions only supported b
y particular hardware devices.

Tony H.
Measurement Studio
0 Kudos
Message 4 of 6
(5,095 Views)
I experienced the same problem, found this solution and attempted to call MyTask.Control(TaskAction.Abort) from a Stop button on my form. But evidently it ignores the Stop event. I also use PFIO to trigger a Analog ReadSingleSample method and it seems to hang there. At present, it is a single task, single threaded program. How can I make this work?
0 Kudos
Message 5 of 6
(5,095 Views)
In this case, you are calling ReadSingleSample on your application's main thread. This thread also handles the user interface. Because ReadSingleSample will block this thread until the read completes or stops due to an error, it will also cause your user interface to be unresponsive until the read completes. The application is not ignoring the stop event, but Windows is forced to queue up the event and wait until your application's main thread is free again before it can process the event. However, this won't happen until the read completes or times out and frees the thread.

To get around this problem, you need to use the asynchronous BeginReadSingleSample/EndReadSingleSample methods. You can do this without the headaches
of writing a multi-threaded program however. If you set the SynchronizingObject property on your reader class to your main form instance ("this" in C# or "Me" in VB .NET if you are writing all of your code in the form), then the callback you specify to BeginRead will occur in the main thread when the data is ready, in much the same way as the button callback occurs when you press a button. When the callback occurs, you can call EndRead without the thread blocking because you know the data has already been acquired and is ready to be retrieved.

It sounds like the example installed at at Program Files\National Instruments\MeasurementStudio70\DotNET\Examples\DAQmx\Analog In\Measure Voltage\AcqVoltageSamples_IntClkAnalogStart might be pretty close to what you are trying to do.

Let me know if you have any more questions.

Tony H.
Measurement Studio
0 Kudos
Message 6 of 6
(5,095 Views)