public static int HandleWork <T>(IConsole console, WorkDescription currentWork, ILogger logger, Func <WorkDescription, string, IEvent[]> callback) where T : IEvent { Guard.ArgumentNotNull(currentWork, nameof(currentWork)); Guard.ArgumentNotNull(logger, nameof(logger)); Guard.ArgumentNotNull(currentWork.Channels, "currentWork.Channels"); List <IEvent> FullList = new List <IEvent>(); List <Task <Tuple <IEvent[], TimeSpan> > > TaskList = new List <Task <Tuple <IEvent[], TimeSpan> > >(); List <Task> continuations = new List <Task>(); foreach (string item in currentWork.Items) { lock (console) { console.WriteLine(string.Format(" {0} Task started...", item), null, null); } Task <Tuple <IEvent[], TimeSpan> > task = Task.Factory.StartNew <Tuple <IEvent[], TimeSpan> >(() => { Stopwatch sw = Stopwatch.StartNew(); IEvent[] ev = callback(currentWork, item); sw.Stop(); return(new Tuple <IEvent[], TimeSpan>(ev, sw.Elapsed)); }); Task ct = task.ContinueWith((prec) => { lock (console) { console.WriteLine(string.Format(" {0} Task completed", item), null, null); console.WriteLine(string.Format(" {0} Events identified in {1}ms", prec.Result.Item1.Length, Convert.ToInt64(prec.Result.Item2.TotalMilliseconds)), null, null); } }, TaskContinuationOptions.NotOnFaulted); TaskList.Add(task); continuations.Add(ct); } Task.WaitAll(TaskList.ToArray()); Task.WaitAll(continuations.ToArray()); NotificationBuilder nbuilder = new NotificationBuilder().UseHtmlRenderer(); //Date Hierarchy (Year/Month/Day) created by Blogger, we must only provide a meaningfull subject string itemsAsString = string.Join(",", currentWork.Items); nbuilder.UseSubject(string.Format("{0} {1}", itemsAsString, Pluralize(currentWork.ItemType.ToString()))); foreach (Task <Tuple <IEvent[], TimeSpan> > t in TaskList) { nbuilder.AddEvents(t.Result.Item1); } if (nbuilder.HasEvents == false) { console.WriteLine("Now changes found", null, null); return(0); //NO NOTIFICATION required! } nbuilder.AddChannels(currentWork.Channels); Notification noti = nbuilder.Build(); noti.From = currentWork.From; noti.To = currentWork.To; Postman.DeliverNotification(new Notification[] { noti }); console.WriteLine("", null, null); console.WriteLine(string.Format("{0} changes delivered", nbuilder.EventCount), null, null); return(0); }