当前位置: 首页>>代码示例>>C#>>正文


C# AsyncQueue.AcquireReadAsync方法代码示例

本文整理汇总了C#中AsyncQueue.AcquireReadAsync方法的典型用法代码示例。如果您正苦于以下问题:C# AsyncQueue.AcquireReadAsync方法的具体用法?C# AsyncQueue.AcquireReadAsync怎么用?C# AsyncQueue.AcquireReadAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AsyncQueue的用法示例。


在下文中一共展示了AsyncQueue.AcquireReadAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SimpleQueueTestAsync

        private async Task SimpleQueueTestAsync()
        {
            AsyncQueue<int> queue = new AsyncQueue<int>(11);

            Func<Task> producer = async delegate()
            {
                #region

                for (int i = 0; i < 20; ++i)
                {
                    System.Diagnostics.Debug.WriteLine("Acquiring write...");
                    AcquireWriteResult result = await queue.AcquireWriteAsync(1, CancellationToken.None);
                    result.Visit<DBNull>
                    (
                        new Func<AcquireWriteSucceeded, DBNull>
                        (
                            succeeded =>
                            {
                                System.Diagnostics.Debug.WriteLine("Write-acquire succeeded, offset " + succeeded.Offset + ", acquired " + succeeded.ItemCount + " spaces");

                                Assert.AreEqual(1, succeeded.ItemCount);
                            
                                if (succeeded.ItemCount >= 1)
                                {
                                    System.Diagnostics.Debug.WriteLine("Releasing write (1)...");
                                    queue.ReleaseWrite(i);
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("Releasing write (0)...");
                                    queue.ReleaseWrite();
                                }

                                return DBNull.Value;
                            }
                        ),
                        new Func<AcquireWriteCancelled, DBNull>
                        (
                            cancelled =>
                            {
                                throw new OperationCanceledException();
                            }
                        ),
                        new Func<AcquireWriteFaulted, DBNull>
                        (
                            faulted =>
                            {
                                throw faulted.Exception;
                            }
                        )
                    );
                }

                System.Diagnostics.Debug.WriteLine("Writing EOF...");

                queue.WriteEof();

                #endregion
            };

            Func<Task> consumer = async delegate()
            {
                #region

                bool more = true;
                while (more)
                {
                    System.Diagnostics.Debug.WriteLine("Acquiring read...");
                    const int ACQUIRE_COUNT = 3;
                    AcquireReadResult result = await queue.AcquireReadAsync(ACQUIRE_COUNT, CancellationToken.None);
                    result.Visit<DBNull>
                    (
                        new Func<AcquireReadSucceeded, DBNull>
                        (
                            succeeded =>
                            {
                                System.Diagnostics.Debug.WriteLine("Read-acquire succeeded, offset " + succeeded.Offset + ", acquired " + succeeded.ItemCount + " items");
                                Assert.IsInstanceOfType(succeeded, typeof(AcquireReadSucceeded<int>));

                                if (succeeded is AcquireReadSucceeded<int>)
                                {
                                    AcquireReadSucceeded<int> succeeded2 = (AcquireReadSucceeded<int>)succeeded;

                                    System.Diagnostics.Debug.WriteLine("{ " + string.Join(", ", succeeded2.Items) + " }");
                                }

                                if (succeeded.ItemCount < ACQUIRE_COUNT)
                                {
                                    System.Diagnostics.Debug.WriteLine("Setting \"more\" flag to false...");
                                    more = false;
                                }

                                System.Diagnostics.Debug.WriteLine("Releasing read (" + succeeded.ItemCount + ")...");
                                queue.ReleaseRead(succeeded.ItemCount);

                                return DBNull.Value;
                            }
                        ),
                        new Func<AcquireReadCancelled, DBNull>
                        (
//.........这里部分代码省略.........
开发者ID:Sunlighter,项目名称:AsyncQueues,代码行数:101,代码来源:AsyncQueueTests.cs


注:本文中的AsyncQueue.AcquireReadAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。