Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. namespace Utils
  6. {
  7. public static class Progress
  8. {
  9. public static Task ProgressDots(CancellationToken token)
  10. {
  11. return ProgressDots(token, 2000);
  12. }
  13.  
  14. public static async Task ProgressDots(CancellationToken token, int msDelay)
  15. {
  16. try
  17. {
  18. await Task.Run(async () =>
  19. {
  20. var dots = ".";
  21. while (!token.IsCancellationRequested)
  22. {
  23. Console.Write($"\r{dots}");
  24. var trimmedLength = dots.Replace("\r", "").Replace("\n", "").Length;
  25. if (trimmedLength%Console.WindowWidth == 0)
  26. {
  27. Console.WriteLine();
  28. dots = "";
  29. }
  30. dots += ".";
  31. await Task.Delay(msDelay, token);
  32. }
  33. },
  34. token);
  35. }
  36. catch
  37. {
  38. /* gulp - task cancelled*/
  39. }
  40. }
  41.  
  42. public static Task ProgressSpinner(CancellationToken token)
  43. {
  44. return ProgressSpinner(token, 100);
  45. }
  46.  
  47. public static async Task ProgressSpinner(CancellationToken token, int msDelay)
  48. {
  49. try
  50. {
  51. await Task.Run(async () =>
  52. {
  53. while (!token.IsCancellationRequested)
  54. {
  55. var arr = new[] {"\\", "|", "/", "-"};
  56. foreach (var a in arr)
  57. {
  58. Console.Write($"\r {a}");
  59. await Task.Delay(msDelay, token);
  60. }
  61. }
  62. },
  63. token);
  64. }
  65. catch
  66. {
  67. /* gulp - task cancelled */
  68. }
  69. finally
  70. {
  71. Console.Write("\r ");
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement