Advertisement
Guest User

Untitled

a guest
May 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10.  
  11. namespace TimePeriod
  12. {
  13. class Program
  14. {
  15. [DllImport("winmm.dll")]
  16. internal static extern uint timeBeginPeriod(uint period);
  17.  
  18. [DllImport("winmm.dll")]
  19. internal static extern uint timeEndPeriod(uint period);
  20.  
  21. const int sleeptime = 5;
  22. static void Main(string[] args)
  23. {
  24. Stopwatch stopwatch = new Stopwatch();
  25. stopwatch.Restart();
  26. Thread.Sleep(sleeptime);
  27. stopwatch.Stop();
  28. Console.WriteLine($"Thread.Sleep({sleeptime}) Normal--{stopwatch.ElapsedMilliseconds}ms--{stopwatch.ElapsedTicks}Ticks");
  29.  
  30. timeBeginPeriod(1);
  31. stopwatch.Restart();
  32. Thread.Sleep(sleeptime);
  33. stopwatch.Stop();
  34. Console.WriteLine($"Thread.Sleep({sleeptime}) timeBeginPeriod(1)--{stopwatch.ElapsedMilliseconds}ms--{stopwatch.ElapsedTicks}Ticks");
  35. timeEndPeriod(1);
  36.  
  37. Task.Run(() =>
  38. {
  39. int count = 50;
  40. while (count > 0)
  41. {
  42. count--;
  43. stopwatch.Restart();
  44. Thread.Sleep(sleeptime);
  45. Console.WriteLine($"Thread.Sleep({sleeptime}) Normal--{stopwatch.ElapsedMilliseconds}ms--{stopwatch.ElapsedTicks}Ticks");
  46. stopwatch.Stop();
  47. }
  48. });
  49.  
  50. Console.ReadLine();
  51.  
  52. }
  53.  
  54.  
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement