你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

IReliableConcurrentQueue<T>.Count 属性

定义

获取 中的 IReliableConcurrentQueue<T>值数。

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

属性值

中的 IReliableConcurrentQueue<T>值数。

例外

副本 (replica) 当前不可读。

示例

此示例演示如何无限监视队列的计数,直到取消令牌。

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);
}

注解

此计数表示当前对 TryDequeueAsync(ITransaction, CancellationToken, Nullable<TimeSpan>)可见的值的数目。 未提交的排队不会增加计数,但未提交的取消排队将减少计数。

请注意,此 API 不采用事务参数。 由于 的影响 TryDequeueAsync(ITransaction, CancellationToken, Nullable<TimeSpan>) 不与其他事务隔离,因此计数也不能与其他事务隔离。

适用于