ThreadTimeoutCallback.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. namespace SAGA.DotNetUtils
  2. {
  3. using System;
  4. using System.Threading;
  5. public class ThreadTimeoutCallback : TimeoutCallback
  6. {
  7. private bool disposeable;
  8. private object ThisSysnObject;
  9. private Timer timer;
  10. public ThreadTimeoutCallback(TimeoutCallbackHandler handler, int timeout, object[] values) : base(handler, timeout, values)
  11. {
  12. this.ThisSysnObject = new object();
  13. }
  14. public override void DisposeCoerce()
  15. {
  16. lock (this.ThisSysnObject)
  17. {
  18. this.disposeable = true;
  19. this.StopTimer();
  20. }
  21. }
  22. protected override bool StartTimer()
  23. {
  24. lock (this.ThisSysnObject)
  25. {
  26. if (this.timer == null)
  27. {
  28. this.timer = new Timer(new TimerCallback(this.TimerCallbackHandler), null, Math.Min(1, base.timeout), 0x124f80);
  29. return true;
  30. }
  31. return false;
  32. }
  33. }
  34. protected override bool StopTimer()
  35. {
  36. if ((this.timer == null) || !this.Disposable)
  37. {
  38. return false;
  39. }
  40. try
  41. {
  42. this.timer.Dispose();
  43. }
  44. catch
  45. {
  46. }
  47. this.timer = null;
  48. return true;
  49. }
  50. private void TimerCallbackHandler(object state)
  51. {
  52. lock (this.ThisSysnObject)
  53. {
  54. if (this.timer != null)
  55. {
  56. try
  57. {
  58. if (base.handler != null)
  59. {
  60. base.handler(base.values);
  61. }
  62. }
  63. catch
  64. {
  65. }
  66. }
  67. this.disposeable = true;
  68. this.StopTimer();
  69. }
  70. }
  71. public override bool Disposable
  72. {
  73. get
  74. {
  75. return this.disposeable;
  76. }
  77. }
  78. }
  79. }