本文整理汇总了C#中ActionBlock.SendAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ActionBlock.SendAsync方法的具体用法?C# ActionBlock.SendAsync怎么用?C# ActionBlock.SendAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ActionBlock
的用法示例。
在下文中一共展示了ActionBlock.SendAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProduceLogs
public void ProduceLogs(int count, int buffSize)
{
var bufferOptions = new DataflowBlockOptions() { BoundedCapacity = buffSize };
var writerOptions = new ExecutionDataflowBlockOptions() { BoundedCapacity = 10, MaxDegreeOfParallelism = 1, MaxMessagesPerTask = 10, SingleProducerConstrained = true };
LogGenerator g = new LogGenerator();
var file = new StreamWriter("basic.async.buff.log", false);
BufferBlock<string> buffer = new BufferBlock<string>(bufferOptions);
ActionBlock<string> writer = new ActionBlock<string>(s => file.WriteLine(s), writerOptions);
buffer.LinkTo(writer, new DataflowLinkOptions() { PropagateCompletion = true });
for (int i = 0; i < count; i++)
{
g.Next();
var line = string.Format(g.FormatStr, g.Param1, g.Param2, g.Param3, g.Param4, g.Param5, g.Param6);
writer.SendAsync(line).Wait();
}
buffer.Complete();
Completed = writer.Completion.ContinueWith(t => file.Close());
}
示例2: GetLastEventProcessedForHandlers
private void GetLastEventProcessedForHandlers()
{
//TODO calculate the lowest numbered LEP of all the handlers and use that for the start position
// ask each registered handler to get there last processed event and hold on to it.
var actionBlock = new ActionBlock<IHandler>(x => x.GetLastPositionProcessed(), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 8 });
_eventHandlers.ForEach(async x=> await actionBlock.SendAsync(x));
}
示例3: ControlledTaskScheduler_WaitActionBlock_ThrowException
public void ControlledTaskScheduler_WaitActionBlock_ThrowException() {
Func<object, Task> f = o => Task.Factory.StartNew(SleepAndThrow);
ActionBlock<object> actionBlock = new ActionBlock<object>(f, new ExecutionDataflowBlockOptions { TaskScheduler = _scheduler });
Action a = () => _scheduler.Wait(actionBlock);
actionBlock.SendAsync(null);
a.ShouldThrow<CustomException>();
}
示例4: ActionBlockTest
public async Task ActionBlockTest()
{
var sw = new Stopwatch();
const long ITERS = 100000000;
Console.WriteLine("test count {0:N0}",ITERS);
var are = new AutoResetEvent(false);
var ab = new ActionBlock<int>(i => { if (i == ITERS) are.Set(); });
for(var c=0;c<20;c++)
{
sw.Restart();
for (int i = 1; i <= ITERS; i++)
await ab.SendAsync(i);
are.WaitOne();
sw.Stop();
Console.WriteLine("test {0},Messages / sec: {1:N0} ops,run {2} ms",
c,(ITERS * 1000 / sw.ElapsedMilliseconds), sw.ElapsedMilliseconds);
}
}
示例5: RunActionBlockConformanceTests
//.........这里部分代码省略.........
}
}
Assert.True(localPassed, string.Format("{0}: InputCount", localPassed ? "Success" : "Failure"));
}
// Test ordering
{
bool localPassed = true;
for (int trial = 0; trial < 2; trial++)
{
int prev = -1;
var ab = new ActionBlock<int>(i =>
{
if (prev + 1 != i) localPassed &= false;
prev = i;
}, new ExecutionDataflowBlockOptions { SingleProducerConstrained = (trial == 0) });
for (int i = 0; i < 2; i++) ab.Post(i);
ab.Complete();
ab.Completion.Wait();
}
Assert.True(localPassed, string.Format("{0}: Correct ordering", localPassed ? "Success" : "Failure"));
}
// Test non-greedy
{
bool localPassed = true;
var barrier = new Barrier(2);
var ab = new ActionBlock<int>(i =>
{
barrier.SignalAndWait();
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 1 });
ab.SendAsync(1);
Task.Delay(200).Wait();
var sa2 = ab.SendAsync(2);
localPassed &= !sa2.IsCompleted;
barrier.SignalAndWait(); // for SendAsync(1)
barrier.SignalAndWait(); // for SendAsync(2)
localPassed &= sa2.Wait(100);
int total = 0;
ab = new ActionBlock<int>(i =>
{
Interlocked.Add(ref total, i);
Task.Delay(1).Wait();
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 1 });
for (int i = 1; i <= 100; i++) ab.SendAsync(i);
SpinWait.SpinUntil(() => total == ((100 * 101) / 2), 30000);
localPassed &= total == ((100 * 101) / 2);
Assert.True(localPassed, string.Format("total={0} (must be {1})", total, (100 * 101) / 2));
Assert.True(localPassed, string.Format("{0}: Non-greedy support", localPassed ? "Success" : "Failure"));
}
// Test that OperationCanceledExceptions are ignored
{
bool localPassed = true;
for (int trial = 0; trial < 2; trial++)
{
int sumOfOdds = 0;
var ab = new ActionBlock<int>(i =>
{
if ((i % 2) == 0) throw new OperationCanceledException();
sumOfOdds += i;
}, new ExecutionDataflowBlockOptions { SingleProducerConstrained = (trial == 0) });
for (int i = 0; i < 4; i++) ab.Post(i);
ab.Complete();
示例6: TestReleasingOfPostponedMessages
//[Fact(Skip = "Outerloop")]
public void TestReleasingOfPostponedMessages()
{
const int excess = 5;
for (int dop = 1; dop <= Parallelism.ActualDegreeOfParallelism; dop++)
{
var localPassed = true;
var nextOfferEvent = new AutoResetEvent(true);
var releaseProcessingEvent = new ManualResetEventSlim();
var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop, BoundedCapacity = dop };
var action = new ActionBlock<int>(x => { nextOfferEvent.Set(); releaseProcessingEvent.Wait(); }, options);
var sendAsyncDop = new Task<bool>[dop];
var sendAsyncExcess = new Task<bool>[excess];
// Send DOP messages
for (int i = 0; i < dop; i++)
{
// Throttle sending to make sure we saturate DOP exactly
nextOfferEvent.WaitOne();
sendAsyncDop[i] = action.SendAsync(i);
}
// Send EXCESS more messages. All of these will surely be postponed
for (int i = 0; i < excess; i++)
sendAsyncExcess[i] = action.SendAsync(dop + i);
// Wait until the tasks for the first DOP messages get completed
Task.WaitAll(sendAsyncDop, 5000);
// Complete the block. This will cause the EXCESS messages to be declined.
action.Complete();
releaseProcessingEvent.Set();
// Verify all DOP messages have been accepted
for (int i = 0; i < dop; i++) localPassed &= sendAsyncDop[i].Result;
Assert.True(localPassed, string.Format("DOP={0} : Consumed up to DOP - {1}", dop, localPassed ? "Passed" : "FAILED"));
// Verify all EXCESS messages have been declined
localPassed = true;
for (int i = 0; i < excess; i++) localPassed &= !sendAsyncExcess[i].Result;
Assert.True(localPassed, string.Format("DOP={0} : Declined excess - {1}", dop, localPassed ? "Passed" : "FAILED"));
}
}