Advertisement
Guest User

Untitled

a guest
Jan 9th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Timers;
  7.  
  8.  
  9. class AsyncTimer
  10. {
  11. private Action f;
  12. private int ticks;
  13. private double t;
  14. private int counter;
  15. private Timer aTimer;
  16.  
  17. public Action F
  18. {
  19. get
  20. {
  21. return this.f;
  22. }
  23. set
  24. {
  25. this.f = value;
  26. }
  27. }
  28. public int Ticks
  29. {
  30. get
  31. {
  32. return this.ticks;
  33. }
  34. set
  35. {
  36. if(value<0) throw new ArgumentOutOfRangeException("Ticks can't be less than 0");
  37. this.ticks = value;
  38. }
  39. }
  40. public double T
  41. {
  42. get
  43. {
  44. return this.t;
  45. }
  46. set
  47. {
  48. if(value<0) throw new ArgumentOutOfRangeException("Miliseconds can't be less than 0");
  49. this.t= value;
  50. }
  51. }
  52. public AsyncTimer(Action f, int ticks, double t)
  53. {
  54. this.F = f;
  55. this.Ticks = ticks;
  56. this.T = t;
  57. this.counter = ticks;
  58. this.aTimer = new System.Timers.Timer(this.t);
  59. }
  60. public void Run()
  61. {
  62.  
  63.  
  64. aTimer.Elapsed += new ElapsedEventHandler(OnTimeEvent);
  65.  
  66. aTimer.AutoReset = true;
  67. aTimer.Enabled = true;
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74. private void OnTimeEvent(object source, ElapsedEventArgs e)
  75. {
  76. this.F();
  77. if(this.counter<=1)
  78. {
  79. aTimer.AutoReset = false;
  80. aTimer.Enabled = false;
  81. }
  82. this.counter--;
  83.  
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement