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


C# AsyncQueue.WriteEof方法代码示例

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


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

示例1: CompleteAnyWithCancellationAsync

        private async Task CompleteAnyWithCancellationAsync()
        {
            AsyncQueue<string> q1 = new AsyncQueue<string>(5);
            AsyncQueue<string> q2 = new AsyncQueue<string>(5);

            Func<Task> producer1 = async delegate ()
            {
                await Task.Delay(2500);
                await q1.Enqueue("one", CancellationToken.None);
                q1.WriteEof();
            };

            Func<Task> producer2 = async delegate ()
            {
                await Task.Delay(5500);
                await q2.Enqueue("two", CancellationToken.None);
                q2.WriteEof();
            };

            Func<Task> consumer = async delegate ()
            {
                bool q1eof = false;
                bool q2eof = false;
                while (!q1eof || !q2eof)
                {
                    Tuple<int, string> r1 = null;

                    using (CancellationTokenSource s1 = new CancellationTokenSource(1000))
                    {
                        try
                        {
                            r1 = await Utils.OperationStarters<int, string>()
                                .AddIf(!q1eof, 1, Utils.StartableGet(q1, x => x, null))
                                .AddIf(!q2eof, 2, Utils.StartableGet(q2, x => x, null))
                                .CompleteAny(s1.Token);
                        }
                        catch (OperationCanceledException)
                        {

                        }
                    }

                    if (r1 == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Cancelled (because of timeout)");
                    }
                    else if (r1.Item1 == 1)
                    {
                        if (r1.Item2 == null)
                        {
                            System.Diagnostics.Debug.WriteLine("Got EOF from 1");
                            q1eof = true;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine($"Got {r1.Item2} from 1");
                        }
                    }
                    else
                    {
                        Assert.AreEqual(2, r1.Item1);
                        if (r1.Item2 == null)
                        {
                            System.Diagnostics.Debug.WriteLine("Got EOF from 2");
                            q2eof = true;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine($"Got {r1.Item2} from 2");
                        }
                    }
                }
            };

            Task p1 = Task.Run(producer1);
            Task p2 = Task.Run(producer2);
            Task c = Task.Run(consumer);

            await Task.WhenAll(p1, p2, c);
        }
开发者ID:Sunlighter,项目名称:AsyncQueues,代码行数:80,代码来源:AsyncQueueTests.cs

示例2: ExtMethodQueueTestAsync

        private async Task ExtMethodQueueTestAsync()
        {
            AsyncQueue<string> q = new AsyncQueue<string>(5);

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

                for (int i = 0; i < 20; ++i)
                {
                    System.Diagnostics.Debug.WriteLine($"Writing {i}...");
                    await q.Enqueue($"Value: {i}", CancellationToken.None);
                }

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

                q.WriteEof();

                #endregion
            };

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

                bool more = true;
                while (more)
                {
                    System.Diagnostics.Debug.WriteLine("Reading...");
                    Option<string> ostr = await q.Dequeue(CancellationToken.None);

                    if (ostr.HasValue)
                    {
                        System.Diagnostics.Debug.WriteLine($"Read {ostr.Value}");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Read EOF...");
                        more = false;
                    }

                }

                #endregion
            };

            Task tProducer = Task.Run(producer);
            Task tConsumer = Task.Run(consumer);

            await Task.WhenAll(tProducer, tConsumer);
        }
开发者ID:Sunlighter,项目名称:AsyncQueues,代码行数:51,代码来源:AsyncQueueTests.cs

示例3: PutItems

        private async Task PutItems(string prefix, AsyncQueue<string> dest, List<Item> items)
        {
            foreach(Item i in items)
            {
                await Task.Delay(i.delay);
                await dest.Enqueue(prefix + i.value, CancellationToken.None);
            }

            dest.WriteEof();
        }
开发者ID:Sunlighter,项目名称:AsyncQueues,代码行数:10,代码来源:AsyncQueueTests.cs

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