本文整理汇总了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));
}
}
示例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));
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
}