2 // System.Web.TimeoutManager
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Novell, Inc (http://www.novell.com)
11 using System.Collections;
12 using System.Threading;
13 using System.Web.Configuration;
26 public TimeoutManager ()
28 contexts = Hashtable.Synchronized (new Hashtable ());
29 timer = new Timer (new TimerCallback (CheckTimeouts), null, 0, 15000);
32 public void Add (HttpContext context)
34 object value = contexts [context];
36 value = Thread.CurrentThread;
37 } else if (value is Thread) {
38 ArrayList list = new ArrayList ();
40 list.Add (Thread.CurrentThread);
43 ArrayList list = (ArrayList) value;
44 list.Add (Thread.CurrentThread);
49 contexts [context] = value;
53 public Thread Remove (HttpContext context)
55 object value = contexts [context];
59 if (value is Thread) {
61 contexts.Remove (context);
63 return (Thread) value;
66 ArrayList list = (ArrayList) value;
69 result = (Thread) list [list.Count - 1];
70 list.RemoveAt (list.Count - 1);
73 if (list.Count == 0) {
75 contexts.Remove (context);
82 void CheckTimeouts (object state)
84 if (contexts.Count == 0) {
88 DateTime now = DateTime.Now;
89 ArrayList clist = new ArrayList ();
91 lock (this) { // The lock prevents Keys enumerator from being out of synch
92 clist.AddRange (contexts.Keys);
95 foreach (HttpContext context in clist) {
96 if (!context.CheckIfTimeout (now))
99 Thread thread = Remove (context);
100 if (thread != null) // Only if context is removed right after the lock
101 thread.Abort (new StepTimeout ());