using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace SketchAssistantWPF { public class RedrawManager { public int radius { get; private set; } private RedrawLine[] redrawLines; public int currentLine { get; private set; } public bool finished { get; private set; } public RedrawManager(List linesToRedraw) { radius = 20; redrawLines = new RedrawLine[linesToRedraw.Count]; Task[] taskPool = new Task[linesToRedraw.Count]; Console.WriteLine("STARTED THREAD CREATION"); Console.WriteLine("Processor Count {0}", Environment.ProcessorCount); for(int i = 0; i < linesToRedraw.Count; i++) { InternalLine line = linesToRedraw[i]; redrawLines[i] = (new RedrawLine(radius)); object arg = new Tuple (redrawLines[i], line); taskPool[i] = Task.Factory.StartNew(new Action((x) => { ((Tuple)arg).Item1.Init(((Tuple)arg).Item2); }),arg); } Console.WriteLine("STARTED {0} THREADS", linesToRedraw.Count); Task.WaitAll(taskPool); Console.WriteLine("FINISHED ALL THREADS"); finished = false; currentLine = 0; } public Tuple GetOverlayPosition() { if(currentLine < 0) { return new Tuple(false, new Point(0, 0)); } else { return new Tuple(true, redrawLines[currentLine].GetOverlayPosition()); } } public double GetDirection(Point p) { if (finished) return -1; double dir = redrawLines[currentLine].GetDirection(p); if(dir < 0) { currentLine++; finished = true; } return dir; } } }