ConfigurePileline.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections.Concurrent;
  2. using System.Diagnostics;
  3. namespace etoy
  4. {
  5. class ConfigurePileline
  6. {
  7. ConcurrentQueue<Command> _commands;
  8. ConcurrentQueue<Command> _completeds;
  9. Thread _thread;
  10. int _total;
  11. Stopwatch _cmdStopwatch;
  12. int _top;
  13. public ConfigurePileline(IEnumerable<Command> commands)
  14. {
  15. _commands = new ConcurrentQueue<Command>(commands);
  16. _total = _commands.Count;
  17. _completeds = new ConcurrentQueue<Command>();
  18. _cmdStopwatch = new Stopwatch();
  19. InitThread();
  20. }
  21. public void Run()
  22. {
  23. _thread.Start();
  24. }
  25. void InitThread()
  26. {
  27. _thread = new Thread(OnRunThread);
  28. }
  29. void ProgressBar(ICommand cmd)
  30. {
  31. Console.SetCursorPosition(0, _top);
  32. Console.Write(new string(' ', Console.WindowWidth));
  33. Console.SetCursorPosition(0, _top);
  34. var str1 = new string('#', (int)(50 * cmd.Progress));
  35. var str2 = new string(' ', 50 - (int)(50 * cmd.Progress));
  36. float progress = 100 * (cmd.Progress > 0.999f ? 1 : cmd.Progress);
  37. string cost = $"耗时:{_cmdStopwatch.ElapsedMilliseconds}ms";
  38. Console.ForegroundColor = ConsoleColor.Blue;
  39. Console.Write(string.Format(">> [{0}{1}] {2:f2}% {3}", str1, str2, progress, cost));
  40. Console.ForegroundColor = ConsoleColor.White;
  41. }
  42. void OnRunThread(object obj)
  43. {
  44. Stopwatch sw = new Stopwatch();
  45. sw.Start();
  46. bool success = true;
  47. Console.WriteLine(">> 开始执行");
  48. Console.WriteLine();
  49. while (_commands.TryPeek(out var command))
  50. {
  51. if (command is ICommand cmd)
  52. {
  53. _top = Console.CursorTop;
  54. if (cmd.Done)
  55. {
  56. _cmdStopwatch.Stop();
  57. ProgressBar(cmd);
  58. Console.WriteLine();
  59. if (_commands.TryDequeue(out var completedCommand))
  60. _completeds.Enqueue(completedCommand);
  61. }
  62. else
  63. {
  64. try
  65. {
  66. if (!cmd.Start)
  67. {
  68. Console.WriteLine($">> 步骤 ({_completeds.Count + 1}/{_total}) [{command.Description}]");
  69. _cmdStopwatch.Restart();
  70. cmd.Execute();
  71. }
  72. else
  73. {
  74. if (cmd.Exception != null)
  75. throw cmd.Exception;
  76. ProgressBar(cmd);
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. Console.ForegroundColor = ConsoleColor.Red;
  82. Console.WriteLine();
  83. Console.WriteLine($"## CMD异常 Type:{cmd.GetType()}\nMessage:{e.Message}");
  84. Console.ForegroundColor = ConsoleColor.White;
  85. success = false;
  86. break;
  87. }
  88. }
  89. }
  90. Thread.Sleep(5);
  91. }
  92. sw.Stop();
  93. Console.WriteLine();
  94. if (success)
  95. {
  96. Console.ForegroundColor = ConsoleColor.Green;
  97. Console.WriteLine($">> 执行成功. 总耗时: {sw.ElapsedMilliseconds} ms");
  98. Console.ForegroundColor = ConsoleColor.White;
  99. }
  100. else
  101. {
  102. Console.ForegroundColor = ConsoleColor.Red;
  103. Console.WriteLine($">> 执行失败. ");
  104. Console.ForegroundColor = ConsoleColor.White;
  105. //输入enter键才关闭控制台
  106. Console.ReadLine();
  107. }
  108. }
  109. }
  110. }