Watcher.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // System.Web.Caching.Watcher
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  8. //
  9. // Use this until we have a FileSystemWatcher...
  10. using System;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Threading;
  14. namespace System.Web.Caching
  15. {
  16. class Watcher
  17. {
  18. static Hashtable watches;
  19. static Watcher worker;
  20. bool running;
  21. Thread t;
  22. private Watcher ()
  23. {
  24. watches = new Hashtable ();
  25. t = new Thread (new ThreadStart (Run));
  26. t.IsBackground = true;
  27. t.Start ();
  28. }
  29. static int CalculateSleep ()
  30. {
  31. if (watches == null || watches.Count == 0)
  32. return 5000;
  33. int result = (int) Math.Pow (10, Math.Log (watches.Count, 10) + 1.75);
  34. return (result < 500) ? 500 : result;
  35. }
  36. void Run ()
  37. {
  38. ArrayList notified = new ArrayList ();
  39. while (true) {
  40. lock (watches) {
  41. foreach (string key in watches.Keys) {
  42. Watch w = (Watch) watches [key];
  43. if (!w.CheckIfChanged ())
  44. continue;
  45. w.ChangedEvent (this, EventArgs.Empty);
  46. notified.Add (key);
  47. }
  48. foreach (string s in notified)
  49. watches.Remove (s);
  50. notified.Clear ();
  51. }
  52. Thread.Sleep (CalculateSleep ());
  53. }
  54. }
  55. static void EnsureWorker ()
  56. {
  57. if (worker == null)
  58. worker = new Watcher ();
  59. }
  60. static public void AddWatch (string filename, EventHandler eh)
  61. {
  62. EnsureWorker ();
  63. lock (watches) {
  64. if (!watches.Contains (filename)) {
  65. watches.Add (filename, new Watch (filename, eh));
  66. } else {
  67. ((Watch) watches [filename]).AddEvent (eh);
  68. }
  69. }
  70. }
  71. class Watch
  72. {
  73. string filename;
  74. DateTime begin;
  75. EventHandler eh;
  76. bool is_dir;
  77. bool changed;
  78. IsThere exists;
  79. GetTime getTime;
  80. static IsThere fileExists;
  81. static GetTime fileGetTime;
  82. static IsThere dirExists;
  83. static GetTime dirGetTime;
  84. delegate bool IsThere (string filename);
  85. delegate DateTime GetTime (string filename);
  86. static Watch ()
  87. {
  88. fileExists = new IsThere (File.Exists);
  89. fileGetTime = new GetTime (File.GetLastWriteTime);
  90. dirExists = new IsThere (Directory.Exists);
  91. dirGetTime = new GetTime (Directory.GetLastWriteTime);
  92. }
  93. public Watch (string filename, EventHandler eh)
  94. {
  95. this.eh = eh;
  96. this.filename = filename;
  97. is_dir = Directory.Exists (filename);
  98. changed = !(is_dir || File.Exists (filename));
  99. if (is_dir) {
  100. exists = dirExists;
  101. getTime = dirGetTime;
  102. } else {
  103. exists = fileExists;
  104. getTime = fileGetTime;
  105. }
  106. if (!changed)
  107. begin = getTime (filename);
  108. }
  109. public void AddEvent (EventHandler eh)
  110. {
  111. this.eh += eh;
  112. }
  113. public bool CheckIfChanged ()
  114. {
  115. if (changed)
  116. return true;
  117. if (!exists (filename)) {
  118. changed = true;
  119. return true;
  120. }
  121. DateTime current = getTime (filename);
  122. changed = current > begin;
  123. return changed;
  124. }
  125. public EventHandler ChangedEvent {
  126. get { return eh; }
  127. }
  128. }
  129. }
  130. }