Posting to an ActionBlock and waiting for completion
suggest change// Create a block with an asynchronous action
var block = new ActionBlock<string>(async hostName =>
{
IPAddress[] ipAddresses = await Dns.GetHostAddressesAsync(hostName);
Console.WriteLine(ipAddresses[0]);
});
block.Post("google.com"); // Post items to the block's InputQueue for processing
block.Post("reddit.com");
block.Post("stackoverflow.com");
block.Complete(); // Tell the block to complete and stop accepting new items
await block.Completion; // Asynchronously wait until all items have been processed
ActionBlock<T> is in the TPL Dataflow NuGet package, not in the .NET Framework base class library. The await must be inside an async method. GetHostAddressesAsync requires .NET Framework 4.5.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents