Skip to content

Instantly share code, notes, and snippets.

@kadukf
Created November 19, 2013 22:25
Show Gist options
  • Save kadukf/7553682 to your computer and use it in GitHub Desktop.
Save kadukf/7553682 to your computer and use it in GitHub Desktop.
private static readonly BufferBlock<string> Buffer = new BufferBlock<string>();
static void Main(string[] args)
{
Enumerable.Range(0, 100).ToList().ForEach(i => Buffer.SendAsync("Mesage #" + i));
var consumer = new ActionBlock<string>(s => Process(s), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5 });
Buffer.LinkTo(consumer);
Buffer.Completion.ContinueWith(t => consumer.Complete());
Console.WriteLine("Running");
Console.ReadLine();
Buffer.Complete();
consumer.Completion.Wait();
Console.WriteLine("Completed");
Console.ReadLine();
}
private static void Process(string message)
{
Thread.Sleep(250);
string msg = Thread.CurrentThread.ManagedThreadId + " : " + message;
Console.WriteLine(msg);
//messages.Add(msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment