Created
June 8, 2020 15:50
-
-
Save johnterickson/ae5d48ddc808d2ea1b60d9a5789652c3 to your computer and use it in GitHub Desktop.
ActionBlock fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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