Send feedback on this topic.
Teradata.Client.Provider
Asynchronous Data Retrieval
.NET Data Provider for Teradata > Developer's Guide > Working with the Data Provider Features > Asynchronous Support Overview > Asynchronous Data Retrieval

TdDataReader supports the Async methods introduced in .NET Framework 4.5. These methods return a System.Threading.Tasks.Task that may execute asynchronously.

The application may simply await the task to complete, during which time the application thread will not be blocked, or it may continue with other processing and periodically check to see whether the task has completed, by checking its IsCompleted property.

The methods available in TdDataReader are:

The methods available in TdStream are:

The GetStream, GetTextReader and GetXmlReader methods will all return a type that is based on a TdStream. The asynchronous methods provided by these objects will make use of the ReadAsync method in that TdStream.

Guidelines

Each asynchronous method takes an optional System.Threading.CancellationToken that can be used to request cancellation of the operation if it is taking too long. Refer to the description of the specific method to determine the state of the TdDataReader after an asynchronous method has been cancelled.

Only one asynchronous method may be executed at a time within a single TdDataReader.

If asynchronous operations are performed within multiple data readers on the same session simultaneously, those methods may not execute in a completely asynchronous fashion. If there is high network latency when communicating with Teradata, the thread may be blocked for the duration of a round trip message before the method becomes asynchronous.

It is therefore recommended that if an application needs to work with multiple data readers at the same time, it should connect multiple sessions and create a single TdDataReader within each session.

 

Refer to Potential Conflicts when retrieving large amounts of data.

 

The following example illustrates how an application could call one of these methods and then attempt to cancel it:

C#
Copy Code
public void CancelDuringNextResult()
{
    TdCommand cmd = _cn.CreateCommand(MySQL);
    using (TdDataReader reader = cmd.ExecuteReader())
    {
        reader.Read();
        // ... process the data here
        // Skip over the remaining rows to the next resultset
        CancellationTokenSource cts = new CancellationTokenSource();
        Task<bool> t = reader.NextResultAsync(cts.Token);
        while (t.IsCompleted == false)
        {
            Thread.Sleep(1000);
            cts.Cancel(); // Cancel the operation
        }
        // If it is canceled the TdDataReader will be in an invalid state
        if (t.Cancelled == false)
        {
            // It completed before you cancelled it so use it
        }
    }
}