using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; namespace ConcurrentScheduler { class Program { public static BufferBlock<int> m_Queue = new BufferBlock<int>(); static void Main(string[] args) { Task first = Task.Factory.StartNew(() => Console.WriteLine("Begin")); var t = first.ContinueWith((a) => Producer()); // producer task var c = t.ContinueWith((w) => Consumer()); first.Wait(); } private static void Producer() { for (int i = 0; i < 100; i++) { m_Queue.SendAsync(i); } m_Queue.Complete(); Console.WriteLine("There are {0} items in the queue.\n", m_Queue.Count); } private static void Consumer() { try { var consumerBlock = new ActionBlock<int>( data => RunScript(data), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4 }); m_Queue.LinkTo( consumerBlock, new DataflowLinkOptions { PropagateCompletion = true }); consumerBlock.Completion.Wait(); } catch (NullReferenceException ex) { Console.WriteLine("NullReferenceException: " + ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static void RunScript(int number) { try { Thread.Sleep(500); Console.WriteLine(number); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)