var inputBlock = new BufferBlock(); var actionBlock = new ActionBlock (val => Console.WriteLine($"User input: {val}")); inputBlock.LinkTo(actionBlock); Console.WriteLine("Enter first input:"); inputBlock.Post(int.Parse(Console.ReadLine())); Console.WriteLine("Enter second input:"); inputBlock.Post(int.Parse(Console.ReadLine()));
class FileAppendBlock : ITargetBlockThe above examples use the System.Threading.Tasks.Dataflow package library, which provides a set of dataflow primitives to help developers build message passing and parallel processing systems.{ private string filepath; public FileAppendBlock(string filepath) { this.filepath = filepath; } public Task Completion => Task.CompletedTask; public void Complete() { } public void Fault(Exception exception) { } public Task OfferMessageAsync(DataflowMessageHeader messageHeader, string messageValue, ISourceBlock source, bool consumeToAccept) { File.AppendAllText(filepath, messageValue + "\n"); return Task.FromResult(true); } } var filepath = "output.txt"; var appendBlock = new FileAppendBlock(filepath); var inputBlock = new BufferBlock (); inputBlock.LinkTo(appendBlock); inputBlock.Post("Hello"); inputBlock.Post("World"); // check the output file for the appended messages