Skip to content

Instantly share code, notes, and snippets.

@johnterickson
Created June 8, 2020 15:50
Show Gist options
  • Save johnterickson/ae5d48ddc808d2ea1b60d9a5789652c3 to your computer and use it in GitHub Desktop.
Save johnterickson/ae5d48ddc808d2ea1b60d9a5789652c3 to your computer and use it in GitHub Desktop.
ActionBlock fun
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace ActionBlockTest
{
class Program
{
static async Task Main(string[] args)
{
var client = new HttpClient();
client.Timeout = TimeSpan.FromMilliseconds(30 * 1000 /* change this to 1 */);
long totalBytes = 0;
var block = new ActionBlock<Uri>(
async uri =>
{
var bytes = await client.GetByteArrayAsync(uri);
Interlocked.Add(ref totalBytes, bytes.Length);
});
await block.SendAsync(new Uri("https://www.bing.com"));
await block.SendAsync(new Uri("https://www.microsoft.com"));
block.Complete();
await block.Completion;
Console.WriteLine(totalBytes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment