RedrawManager.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. namespace SketchAssistantWPF
  9. {
  10. public class RedrawManager
  11. {
  12. public int radius { get; private set; }
  13. private RedrawLine[] redrawLines;
  14. public int currentLine { get; private set; }
  15. public RedrawManager(List<InternalLine> linesToRedraw)
  16. {
  17. radius = 5;
  18. redrawLines = new RedrawLine[linesToRedraw.Count];
  19. Task[] taskPool = new Task[linesToRedraw.Count];
  20. Console.WriteLine("STARTED THREAD CREATION");
  21. Console.WriteLine("Processor Count {0}", Environment.ProcessorCount);
  22. for(int i = 0; i < linesToRedraw.Count; i++)
  23. {
  24. InternalLine line = linesToRedraw[i];
  25. redrawLines[i] = (new RedrawLine(radius));
  26. object arg = new Tuple<RedrawLine, InternalLine> (redrawLines[i], line);
  27. taskPool[i] = Task.Factory.StartNew(new Action<object>((x) =>
  28. {
  29. ((Tuple<RedrawLine, InternalLine>)arg).Item1.Init(((Tuple<RedrawLine, InternalLine>)arg).Item2);
  30. }),arg);
  31. }
  32. Console.WriteLine("STARTED {0} THREADS", linesToRedraw.Count);
  33. Task.WaitAll(taskPool);
  34. Console.WriteLine("FINISHED ALL THREADS");
  35. currentLine = 0;
  36. }
  37. public Tuple<bool, Point> GetOverlayPosition()
  38. {
  39. if(currentLine < 0)
  40. {
  41. return new Tuple<bool, Point>(false, new Point(0, 0));
  42. }
  43. else
  44. {
  45. return new Tuple<bool, Point>(true, redrawLines[currentLine].GetOverlayPosition());
  46. }
  47. }
  48. public Angle GetDirection(Point p)
  49. {
  50. return redrawLines[currentLine].GetDirection(p);
  51. }
  52. }
  53. }