Condividi tramite


IReliableConcurrentQueue<T>.Count Property

Definition

Gets the number of values in the IReliableConcurrentQueue<T>.

public long Count { get; }
member this.Count : int64
Public ReadOnly Property Count As Long

Property Value

The number of values in the IReliableConcurrentQueue<T>.

Exceptions

The replica is currently not readable.

Examples

This example shows how to monitor the queue's count infinitely, until the cancellation token is canceled.

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var concurrentQueue = await this.StateManager.GetOrAddAsync<IReliableConcurrentQueue<long>>(new Uri("fabric:/concurrentQueue"));

    // Assumption: values are being enqueued/dequeued in another place (e.g. the communication listener).
    var observer = Task.Run(
        async () =>
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    try
                    {
                        Console.WriteLine("Count: " + concurrentQueue.Count);
                    }
                    catch (FabricNotReadableException e)
                    {
                        // Retry until the queue is readable or a different exception is thrown.
                        Console.WriteLine("Queue is not readable, retrying the observation: " + e);
                    }
                    catch (FabricObjectClosedException e)
                    {
                        // Gracefully exit as this is happening due to replica close.
                        Console.WriteLine("Replica is closing, stopping observer: " + e);
                        return;
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
                }
            },
        cancellationToken);
}

Remarks

This count represents the number of values currently visible to TryDequeueAsync(ITransaction, CancellationToken, Nullable<TimeSpan>). Uncommitted Enqueues will not increase the count, however uncommitted Dequeues will decrease the count.

Note that this API does not take a transaction parameter. Since the effects of TryDequeueAsync(ITransaction, CancellationToken, Nullable<TimeSpan>) are not isolated from other transactions, the count also cannot be isolated from other transactions.

Applies to