本文整理汇总了C#中TransformBlock.Receive方法的典型用法代码示例。如果您正苦于以下问题:C# TransformBlock.Receive方法的具体用法?C# TransformBlock.Receive怎么用?C# TransformBlock.Receive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TransformBlock
的用法示例。
在下文中一共展示了TransformBlock.Receive方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunWriteOnceBlockConformanceTests
public void RunWriteOnceBlockConformanceTests()
{
bool passed = true, localPassed = true;
{
// Test posting then receiving
localPassed = true;
var wob = new WriteOnceBlock<int>(i => i);
int successfulPosts = 0;
for (int i = 10; i <= 20; i++)
{
successfulPosts += wob.Post(i) ? 1 : 0;
}
localPassed |= successfulPosts == 1;
localPassed |= wob.Receive() == 10;
Console.WriteLine("{0}: Posting then receiving", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
{
// Test receiving then posting
localPassed = true;
var wob = new WriteOnceBlock<int>(i => i);
Task.Factory.StartNew(() =>
{
Task.Delay(1000).Wait();
wob.Post(42);
});
localPassed |= wob.Receive() == 42;
localPassed |= wob.Post(43) == false;
wob.Completion.Wait();
Console.WriteLine("{0}: Receiving then posting", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
{
// Test broadcasting
localPassed = true;
var wob = new WriteOnceBlock<int>(i => i + 1);
var tb1 = new TransformBlock<int, int>(i => i);
var tb2 = new TransformBlock<int, int>(i => i);
var tb3 = new TransformBlock<int, int>(i => i);
wob.LinkTo(tb1);
wob.LinkTo(tb2);
wob.LinkTo(tb3);
wob.Post(42);
localPassed |= tb1.Receive() == 43;
localPassed |= tb2.Receive() == 43;
localPassed |= tb3.Receive() == 43;
Console.WriteLine("{0}: Broadcasting", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
{
// Test using a precanceled token
localPassed = true;
try
{
var cts = new CancellationTokenSource();
cts.Cancel();
var dbo = new DataflowBlockOptions { CancellationToken = cts.Token };
var wob = new WriteOnceBlock<int>(i => i, dbo);
int ignoredValue;
IList<int> ignoredValues;
localPassed &= wob.LinkTo(new ActionBlock<int>(delegate { })) != null;
localPassed &= wob.SendAsync(42).Result == false;
localPassed &= ((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues) == false;
localPassed &= wob.Post(42) == false;
localPassed &= wob.TryReceive(out ignoredValue) == false;
localPassed &= wob.Completion != null;
wob.Complete();
}
catch (Exception)
{
localPassed = false;
}
Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
{
// Test using token canceled after construction
localPassed = true;
try
{
var cts = new CancellationTokenSource();
var dbo = new DataflowBlockOptions { CancellationToken = cts.Token };
var wob = new WriteOnceBlock<int>(i => i, dbo);
cts.Cancel();
int ignoredValue;
IList<int> ignoredValues;
localPassed &= wob.LinkTo(new ActionBlock<int>(delegate { })) != null;
localPassed &= wob.SendAsync(42).Result == false;
localPassed &= ((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues) == false;
localPassed &= wob.Post(42) == false;
localPassed &= wob.TryReceive(out ignoredValue) == false;
localPassed &= wob.Completion != null;
wob.Complete();
}
//.........这里部分代码省略.........
示例2: RunTransformBlockConformanceTests
//[Fact(Skip = "Outerloop")]
public void RunTransformBlockConformanceTests()
{
bool passed = true;
// SYNC
#region Sync
{
// Do everything twice - once through OfferMessage and Once through Post
for (FeedMethod feedMethod = FeedMethod._First; passed & feedMethod < FeedMethod._Count; feedMethod++)
{
Func<DataflowBlockOptions, TargetProperties<int>> transformBlockFactory =
options =>
{
TransformBlock<int, int> transformBlock = new TransformBlock<int, int>(i => i, (ExecutionDataflowBlockOptions)options);
ActionBlock<int> actionBlock = new ActionBlock<int>(i => TrackCaptures(i), (ExecutionDataflowBlockOptions)options);
transformBlock.LinkTo(actionBlock);
return new TargetProperties<int> { Target = transformBlock, Capturer = actionBlock, ErrorVerifyable = false };
};
CancellationTokenSource cancellationSource = new CancellationTokenSource();
var defaultOptions = new ExecutionDataflowBlockOptions();
var dopOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
var mptOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, MaxMessagesPerTask = 2 };
var cancellationOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, MaxMessagesPerTask = 2, CancellationToken = cancellationSource.Token };
passed &= FeedTarget(transformBlockFactory, defaultOptions, 1, Intervention.None, null, feedMethod, true);
passed &= FeedTarget(transformBlockFactory, defaultOptions, 1, Intervention.None, null, feedMethod, true);
passed &= FeedTarget(transformBlockFactory, dopOptions, 1, Intervention.None, null, feedMethod, true);
passed &= FeedTarget(transformBlockFactory, mptOptions, 1, Intervention.None, null, feedMethod, true);
passed &= FeedTarget(transformBlockFactory, mptOptions, 1, Intervention.Complete, null, feedMethod, true);
passed &= FeedTarget(transformBlockFactory, cancellationOptions, 1, Intervention.Cancel, cancellationSource, feedMethod, true);
}
// Test chained Post/Receive
{
bool localPassed = true;
const int ITERS = 2;
var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
for (int i = 0; i < ITERS; i++)
{
network.Post(i);
localPassed &= (((IReceivableSourceBlock<int>)network).Receive() == i * 16);
}
Console.WriteLine("{0}: Chained Post/Receive", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
// Test chained SendAsync/Receive
{
bool localPassed = true;
const int ITERS = 2;
var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
for (int i = 0; i < ITERS; i++)
{
network.SendAsync(i);
localPassed &= (((IReceivableSourceBlock<int>)network).Receive() == i * 16);
}
Console.WriteLine("{0}: Chained SendAsync/Receive", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
// Test chained Post all then Receive
{
bool localPassed = true;
const int ITERS = 2;
var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
for (int i = 0; i < ITERS; i++) localPassed &= network.Post(i) == true;
for (int i = 0; i < ITERS; i++) localPassed &= ((IReceivableSourceBlock<int>)network).Receive() == i * 16;
Console.WriteLine("{0}: Chained Post all then Receive", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
// Test chained SendAsync all then Receive
{
bool localPassed = true;
const int ITERS = 2;
var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
var tasks = new Task[ITERS];
for (int i = 1; i <= ITERS; i++) tasks[i - 1] = network.SendAsync(i);
Task.WaitAll(tasks);
int total = 0;
for (int i = 1; i <= ITERS; i++) total += ((IReceivableSourceBlock<int>)network).Receive();
localPassed &= (total == ((ITERS * (ITERS + 1)) / 2 * 16));
Console.WriteLine("{0}: Chained SendAsync all then Receive", localPassed ? "Success" : "Failure");
passed &= localPassed;
}
// Test that OperationCanceledExceptions are ignored
{
bool localPassed = true;
var t = new TransformBlock<int, int>(i =>
{
if ((i % 2) == 0) throw new OperationCanceledException();
return i;
});
for (int i = 0; i < 2; i++) t.Post(i);
t.Complete();
//.........这里部分代码省略.........
示例3: RunBroadcastBlockConformanceTests
//[Fact(Skip = "Outerloop")]
public void RunBroadcastBlockConformanceTests()
{
bool localPassed = true;
{
// Test posting then receiving
localPassed = true;
var bb = new BroadcastBlock<int>(i => i);
for (int i = 0; i < 2; i++) bb.Post(i);
Task.Delay(1).Wait();
localPassed |= bb.Receive() == 1;
Assert.True(localPassed, string.Format("{0}: Posting then receiving", localPassed ? "Success" : "Failure"));
}
{
// Test receiving then posting
localPassed = true;
var bb = new BroadcastBlock<int>(i => i);
Task.Factory.StartNew(() =>
{
Task.Delay(1).Wait();
bb.Post(42);
});
localPassed |= bb.Receive() == 42;
Assert.True(localPassed, string.Format("{0}: Receiving then posting", localPassed ? "Success" : "Failure"));
}
{
// Test broadcasting
localPassed = true;
var bb = new BroadcastBlock<int>(i => i + 1);
var tb1 = new TransformBlock<int, int>(i => i);
var tb2 = new TransformBlock<int, int>(i => i);
var tb3 = new TransformBlock<int, int>(i => i);
bb.LinkTo(tb1);
bb.LinkTo(tb2);
bb.LinkTo(tb3);
for (int i = 0; i < 2; i++)
{
bb.Post(i);
}
for (int i = 0; i < 2; i++)
{
localPassed |= tb1.Receive() == i + 1;
localPassed |= tb2.Receive() == i + 1;
localPassed |= tb3.Receive() == i + 1;
}
Assert.True(localPassed, string.Format("{0}: Broadcasting", localPassed ? "Success" : "Failure"));
}
// Test using a precanceled token
{
localPassed = true;
try
{
var cts = new CancellationTokenSource();
cts.Cancel();
var dbo = new DataflowBlockOptions { CancellationToken = cts.Token };
var bb = new BroadcastBlock<int>(i => i, dbo);
int ignoredValue;
IList<int> ignoredValues;
localPassed &= bb.LinkTo(new ActionBlock<int>(delegate { })) != null;
localPassed &= bb.SendAsync(42).Result == false;
localPassed &= ((IReceivableSourceBlock<int>)bb).TryReceiveAll(out ignoredValues) == false;
localPassed &= bb.Post(42) == false;
localPassed &= bb.TryReceive(out ignoredValue) == false;
localPassed &= bb.Completion != null;
bb.Complete();
}
catch (Exception)
{
localPassed = false;
}
Assert.True(localPassed, string.Format("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"));
}
}