public async Task TestDataBroadcaster1() { var random = new Random(); var dataCopier = new DataBroadcaster<int>(); int sum1 = 0; int sum2 = 0; var action1 = new ActionBlock<int>(i => sum1 = sum1 + i); var action2 = new ActionBlock<int>(i => sum2 = sum2 + i); dataCopier.LinkTo(DataflowUtils.FromBlock(action1)); dataCopier.LinkTo(DataflowUtils.FromBlock(action2)); for (int j = 0; j < 1000; j++) { dataCopier.InputBlock.Post((int) (random.NextDouble()*10000)); } dataCopier.Complete(); await TaskEx.AwaitableWhenAll(action1.Completion, action2.Completion); Console.WriteLine("sum1 = {0} , sum2 = {1}", sum1, sum2); Assert.AreEqual(sum1, sum2); }
public async Task TestDataBroadcaster1() { var random = new Random(); var dataCopier = new DataBroadcaster <int>(); int sum1 = 0; int sum2 = 0; var action1 = new ActionBlock <int>(i => sum1 = sum1 + i); var action2 = new ActionBlock <int>(i => sum2 = sum2 + i); dataCopier.LinkTo(DataflowUtils.FromBlock(action1)); dataCopier.LinkTo(DataflowUtils.FromBlock(action2)); for (int j = 0; j < 1000; j++) { dataCopier.InputBlock.Post((int)(random.NextDouble() * 10000)); } dataCopier.InputBlock.Complete(); await TaskEx.AwaitableWhenAll(action1.Completion, action2.Completion); Console.WriteLine("sum1 = {0} , sum2 = {1}", sum1, sum2); Assert.AreEqual(sum1, sum2); }
public static async Task BroadcasterDemo() { var broadcaster = new DataBroadcaster <string>(); var printer1 = new ActionBlock <string>(s => Console.WriteLine("Printer1: {0}", s)).ToDataflow(); var printer2 = new ActionBlock <string>(s => Console.WriteLine("Printer2: {0}", s)).ToDataflow(); var printer3 = new ActionBlock <string>(s => Console.WriteLine("Printer3: {0}", s)).ToDataflow(); broadcaster.LinkTo(printer1); broadcaster.LinkTo(printer2); broadcaster.LinkTo(printer3); broadcaster.Post("first message"); broadcaster.Post("second message"); broadcaster.Post("third message"); await broadcaster.SignalAndWaitForCompletionAsync(); await printer1.CompletionTask; await printer2.CompletionTask; await printer3.CompletionTask; }
public static async Task BroadcasterDemo() { var broadcaster = new DataBroadcaster<string>(); var printer1 = new ActionBlock<string>(s => Console.WriteLine("Printer1: {0}", s)).ToDataflow(); var printer2 = new ActionBlock<string>(s => Console.WriteLine("Printer2: {0}", s)).ToDataflow(); var printer3 = new ActionBlock<string>(s => Console.WriteLine("Printer3: {0}", s)).ToDataflow(); broadcaster.LinkTo(printer1); broadcaster.LinkTo(printer2); broadcaster.LinkTo(printer3); broadcaster.Post("first message"); broadcaster.Post("second message"); broadcaster.Post("third message"); await broadcaster.SignalAndWaitForCompletionAsync(); await printer1.CompletionTask; await printer2.CompletionTask; await printer3.CompletionTask; }