Asynchronous Reads and Writes

NI-DAQmx 17.1 .NET Class Library Help

Edition Date: June 2017

Part Number: 370473J-01

»View Product Info
Download Help (Windows Only)
Note Note   The NI-DAQmx library follows the .NET Framework asynchronous programming model (APM) or IAsyncResult pattern for asynchronous operations. Refer to Asynchronous Programming Model (APM) in the .NET Framework Developer's Guide for more information.

You start an asynchronous operation in the NI-DAQmx library by calling a BeginRead or BeginWrite method on a reader or writer class. This method returns an instance of the IAsyncResult interface that represents the asynchronous operation. This interface provides methods for determining if the operation has completed, for waiting until the operation is complete, and other operations. Optionally, you can pass a delegate to the BeginRead or BeginWrite method if you want to receive a callback when the operation completes.

Call an EndRead or EndWrite method on the reader or writer class to finish an asynchronous operation. Call the EndRead or EndWrite method that matches the BeginRead or BeginWrite method that you called to start the operation. When the EndRead or EndWrite method is called, any exception that occurred during the asynchronous operation is thrown. If the operation is not yet complete, EndRead or EndWrite waits for it to complete. In the case of a read operation, the data that was read is returned from the EndRead method. You can call the EndRead or EndWrite method from inside a callback, as shown in the following example:

VB.NET

Class DAQmxAsyncRead

    Private reader As AnalogSingleChannelReader

    Public Sub New(ByVal myTask As Task)
        'Create the reader
        reader = New AnalogSingleChannelReader(myTask.Stream)
        'Acquire 100 samples 
        Dim handle As IAsyncResult
        handle = reader.BeginReadMultiSample(100, AddressOf OnDataReady, Nothing)
    End Sub 

    Public Sub OnDataReady(i As IAsyncResult)
        'Retrieve the data that was read. 
        'At this point, any exceptions that occurred during the asynchronous read are thrown 
        Dim data As Double()
        data = reader.EndReadMultiSample(i)

        'You can call the BeginReadMultiSample method here again 
    End Sub 

End Class

C#

class DAQmxAsyncRead
{
    private AnalogSingleChannelReader reader = null;

    public DAQmxAsyncRead(Task myTask)
    {
        // Create the reader.
        reader = new AnalogSingleChannelReader(myTask.Stream);
        // Acquire 100 samples.
        IAsyncResult handle = reader.BeginReadMultiSample(100, OnDataReady, null);
    }

    public void OnDataReady(IAsyncResult i)
    {
        // Retrieve the data that was read. 
        // At this point, any exceptions that occurred during the asynchronous read are thrown.
        double[] data = reader.EndReadMultiSample(i);

        // You can call the BeginReadMultiSample method here again.
    }
}
Note Note   In some cases, callbacks and event handlers are executed in a different thread than the rest of the program. Therefore, you must take special care when accessing objects that have thread affinity, such as UI controls, from these callbacks and event handlers.
Related concepts

WAS THIS ARTICLE HELPFUL?

Not Helpful