RedrawManager.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 bool finished { get; private set; }
  16. public RedrawManager(List<InternalLine> linesToRedraw)
  17. {
  18. radius = 20;
  19. redrawLines = new RedrawLine[linesToRedraw.Count];
  20. Task[] taskPool = new Task[linesToRedraw.Count];
  21. Console.WriteLine("STARTED THREAD CREATION");
  22. Console.WriteLine("Processor Count {0}", Environment.ProcessorCount);
  23. for(int i = 0; i < linesToRedraw.Count; i++)
  24. {
  25. InternalLine line = linesToRedraw[i];
  26. redrawLines[i] = (new RedrawLine(radius));
  27. object arg = new Tuple<RedrawLine, InternalLine> (redrawLines[i], line);
  28. taskPool[i] = Task.Factory.StartNew(new Action<object>((x) =>
  29. {
  30. ((Tuple<RedrawLine, InternalLine>)arg).Item1.Init(((Tuple<RedrawLine, InternalLine>)arg).Item2);
  31. }),arg);
  32. }
  33. Console.WriteLine("STARTED {0} THREADS", linesToRedraw.Count);
  34. Task.WaitAll(taskPool);
  35. Console.WriteLine("FINISHED ALL THREADS");
  36. finished = false;
  37. currentLine = 0;
  38. }
  39. public Tuple<bool, Point> GetOverlayPosition()
  40. {
  41. if(currentLine < 0)
  42. {
  43. return new Tuple<bool, Point>(false, new Point(0, 0));
  44. }
  45. else
  46. {
  47. return new Tuple<bool, Point>(true, redrawLines[currentLine].GetOverlayPosition());
  48. }
  49. }
  50. public double GetDirection(Point p)
  51. {
  52. if (finished) return -1;
  53. double dir = redrawLines[currentLine].GetDirection(p);
  54. if(dir < 0)
  55. {
  56. currentLine++;
  57. finished = true;
  58. }
  59. return dir;
  60. }
  61. }
  62. }