Events

NI-DAQmx 17.1 .NET Class Library Help

Edition Date: June 2017

Part Number: 370473J-01

»View Product Info
Download Help (Windows Only)

The NI-DAQmx .NET library supports asynchronous notification of various occurrences using .NET events.

Events in the NI-DAQmx .NET library are located on the Task class. The following events are supported:

  • CounterOutput—Occurs when any of the counters used in the task reaches its terminal count.
  • DigitalChangeDetection—Occurs when a digital change is detected on any of the digital lines used in the task.
  • Done—Occurs when the task finishes, either successfully or due to an error.
  • EveryNSampleRead—Occurs when the EveryNSampleReadEventInterval user-defined number of samples is written from the device to the PC buffer. This event works only with devices that support buffered tasks.
  • EveryNSampleWritten—Occurs when the EveryNSamplesWrittenEventInterval user-defined number of samples is written from the PC buffer to the device. This event works only with devices that support buffered tasks.
  • SampleClock—Occurs on each pulse of the task's sample clock.
  • SampleComplete—Occurs when each sample is acquired into the NI-DAQmx driver buffer and is ready for reading.

Asynchronous I/O vs. Events

The NI-DAQmx .NET library also supports asynchronous I/O, which is similar to events. With asynchronous I/O, you create callbacks to be executed when a read or write operation completes. The NI-DAQmx .NET library also supports the EveryNSamples event. With this event, you create callbacks to be executed after a certain number of samples has been processed. You can also use EveryNSamples to call into certain methods of the task and access properties of the task while the task is still running. For more information, refer to Asynchronous Reads and Writes.

Event Handler Considerations

Some events might occur frequently depending on your task configuration. For example, SampleComplete occurs frequently for input tasks with high sampling rates. Your event handler must execute quickly enough to respond to every NI-DAQmx event, as each one occurs. If your event handler cannot keep up with NI-DAQmx, your NI-DAQmx operation might fail or your program might become unresponsive. If you experience these problems, you should shorten your event handler or reconfigure your task so that events occur less often. For example, if your application cannot keep up with the SampleComplete event, you can use asynchronous I/O to execute callbacks after a larger number of samples have been acquired or generated.

Events are registered at the driver level, and as a result, when working with UI, you have to keep in mind that, if the application closes without the task being stopped, events might still occur while the application attempts to close. In such scenarios, the event will try to be executed on a thread that might have ended already. This will yield an error. To avoid this, remember to stop the task before exiting the application.

Event handlers, in some cases, are executed using the SynchronizationContext of the thread you use to execute the handler.

WAS THIS ARTICLE HELPFUL?

Not Helpful