|
static IAsyncEnumerable< LoanedSample< T > > | TakeAsync< T > (this DataReader< T > reader) |
| Provides an infinite asynchronous enumerable that returns new samples from a reader as they become available. More...
|
|
static IAsyncEnumerable< LoanedSample< T > > | TakeAsync< T > (this Selector< T > selector) |
| Provides an infinite asynchronous enumerable that returns new samples from a reader selector as they become available. More...
|
|
static async IAsyncEnumerable< T > | ValidData< T > (this IAsyncEnumerable< LoanedSample< T >> samples, [EnumeratorCancellation] CancellationToken cancellationToken=default) |
| Filters a sequence of samples, returning valid data only. More...
|
|
Provides extensions to DataReader<T>
- Note
- These extensions are included in the
Rti.ConnextDds.Extra
package.
Provides an infinite asynchronous enumerable that returns new samples from a reader as they become available.
- Parameters
-
reader | The reader where the samples come from |
- Template Parameters
-
When an element of the returned IAsyncEnumerable
is requested, if the reader has any data available, it returns it immediately. Otherwise it awaits until new data is available.
The simplest way to use this function is an infinite loop that prints the data as the reader receives it:
await foreach (var sample in reader.TakeAsync())
{
if (sample.Info.ValidData)
{
Console.WriteLine($"Received data: {sample.Data}");
}
}
- Warning
- Each element (LoanedSample<T>) is only valid until the enumerable advances to the next element. The sample or any reference to the sample's fields can no longer be accessed after the enumerable moves to the next sample.
The enumerable can receive a cancellation token, for example:
var cancel = new CancellationTokenSource();
await foreach (var sample in reader.TakeAsync().WithCancellation(cancel.Token))
{
...
}
cancel.Cancel();
- Warning
- The disposal of the DataReader or the Subscriber and DomainParticipant that contains it may fail while the iterator is active. Use a cancellation token to ensure TakeAsync() finishes before disposing said entities.
- See also
- Reading data asynchronously for more code examples.