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


C# TaskFactory.Wait方法代码示例

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


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

示例1: Write_TimeoutUntilRead

        public void Write_TimeoutUntilRead()
        {
            var buffer = new byte[] { 1, 2, 3 };
            using (var stream = new BlockingMemoryStream(boundedCapacity: 2))
            {
                // Write one byte more than the capacity, should cause the write to block.
                var writeTask = new TaskFactory().StartNew(() =>
                    stream.Write(buffer, 0, 3)
                );
                Assert.IsFalse(writeTask.Wait(millisecondsTimeout: 100));

                // Read one byte
                var readByteTask = new TaskFactory().StartNew(() =>
                    stream.ReadByte()
                );
                Assert.IsTrue(readByteTask.Wait(millisecondsTimeout: 1000));
                Assert.AreEqual(1, readByteTask.Result);

                // Now there should be enough buffer space to finish the write task.
                Assert.IsTrue(writeTask.Wait(millisecondsTimeout: 1000));
            }
        }
开发者ID:edvineshagh,项目名称:LatticeUtils,代码行数:22,代码来源:TestBlockingMemoryStream.cs

示例2: WriteThenRead_Timeout

        public void WriteThenRead_Timeout()
        {
            var buffer = new byte[] { 1, 2, 3 };
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(buffer, 0, 3);

                // If we try to read too much, then it should stop blocking and return once we hit the timout.
                var readBuffer = new byte[4];
                var readTask = new TaskFactory().StartNew(() =>
                    stream.Read(readBuffer, offset: 0, count: 4, timeout: TimeSpan.FromMilliseconds(10), cancellationToken: CancellationToken.None)
                );
                Assert.IsTrue(readTask.Wait(millisecondsTimeout: 100));
                var readCount = readTask.Result;

                Assert.AreEqual(3, readCount);
                CollectionAssert.AreEqual(buffer, readBuffer.Take(3));
            }
        }
开发者ID:edvineshagh,项目名称:LatticeUtils,代码行数:19,代码来源:TestBlockingMemoryStream.cs

示例3: WriteThenGetConsumingEnumerable_StopsBlockingWhenCompleteWriting

        public void WriteThenGetConsumingEnumerable_StopsBlockingWhenCompleteWriting()
        {
            var buffer = new byte[] { 1, 2, 3 };
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(buffer, 0, buffer.Length);

                var readToListTask = new TaskFactory().StartNew(() => stream.GetConsumingEnumerable().ToList());
                Assert.IsFalse(readToListTask.Wait(millisecondsTimeout: 100));

                stream.CompleteWriting();

                Assert.IsTrue(readToListTask.Wait(millisecondsTimeout: 1000));
                CollectionAssert.AreEqual(buffer, readToListTask.Result);
            }
        }
开发者ID:edvineshagh,项目名称:LatticeUtils,代码行数:16,代码来源:TestBlockingMemoryStream.cs

示例4: WriteThenReadToEnd_StopsBlockingWhenCompleteWriting

        public void WriteThenReadToEnd_StopsBlockingWhenCompleteWriting()
        {
            var buffer = new byte[] { 1, 2, 3 };
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(buffer, 0, buffer.Length);

                // We can read a byte without hitting any blocking
                var readByteTask = new TaskFactory().StartNew(() => stream.ReadByte());
                Assert.IsTrue(readByteTask.Wait(millisecondsTimeout: 1000));
                Assert.AreEqual(1, readByteTask.Result);

                // If we try to read to the end, then we should block until we call CompleteWriting
                var readToEndTask = new TaskFactory().StartNew(() => StreamUtils.ReadToEnd(stream));
                Assert.IsFalse(readToEndTask.Wait(millisecondsTimeout: 100));

                stream.CompleteWriting();

                Assert.IsTrue(readToEndTask.Wait(millisecondsTimeout: 1000));
                CollectionAssert.AreEqual(buffer.Skip(1), readToEndTask.Result);
            }
        }
开发者ID:edvineshagh,项目名称:LatticeUtils,代码行数:22,代码来源:TestBlockingMemoryStream.cs

示例5: ExecuteTheDelegate

        private void ExecuteTheDelegate(Func<ActionResult> action)
        {
            Task<ActionResult> task = new TaskFactory()
                .StartNew(action);

            if (_config.UseTimeout)
            {
                bool didComplete = task.Wait(_config.Timeout);
                if (!didComplete)
                {
                    throw new CircuitBreakerTimedOutException(_config);
                }
                if (task.Result != ActionResult.Good)
                {
                    throw new ActionResultNotGoodException(_config);
                }
            }
            else
            {
                task.Wait();
                if (task.Result != ActionResult.Good)
                {
                    throw new ActionResultNotGoodException(_config);
                }
            }
        }
开发者ID:simon-taylor-bjss,项目名称:Helpful.CircuitBreaker,代码行数:26,代码来源:CircuitBreaker.cs


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