New test.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / Async / AsyncManager.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc.Async {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Threading;\r
17 \r
18     public class AsyncManager {\r
19 \r
20         private readonly SynchronizationContext _syncContext;\r
21 \r
22         // default timeout is 45 sec\r
23         // from: http://msdn.microsoft.com/en-us/library/system.web.ui.page.asynctimeout.aspx\r
24         private int _timeout = 45 * 1000;\r
25 \r
26         public AsyncManager()\r
27             : this(null /* syncContext */) {\r
28         }\r
29 \r
30         public AsyncManager(SynchronizationContext syncContext) {\r
31             _syncContext = syncContext ?? SynchronizationContextUtil.GetSynchronizationContext();\r
32 \r
33             OutstandingOperations = new OperationCounter();\r
34             OutstandingOperations.Completed += delegate { Finish(); };\r
35 \r
36             Parameters = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);\r
37         }\r
38 \r
39         public OperationCounter OutstandingOperations {\r
40             get;\r
41             private set;\r
42         }\r
43 \r
44         public IDictionary<string, object> Parameters {\r
45             get;\r
46             private set;\r
47         }\r
48 \r
49         public event EventHandler Finished;\r
50 \r
51         // the developer may call this function to signal that all operations are complete instead of\r
52         // waiting for the operation counter to reach zero\r
53         public virtual void Finish() {\r
54             EventHandler handler = Finished;\r
55             if (handler != null) {\r
56                 handler(this, EventArgs.Empty);\r
57             }\r
58         }\r
59 \r
60         // executes a callback in the current synchronization context, which gives access to HttpContext and related items\r
61         public virtual void Sync(Action action) {\r
62             _syncContext.Sync(action);\r
63         }\r
64 \r
65         // measured in milliseconds, Timeout.Infinite means 'no timeout'\r
66         public int Timeout {\r
67             get {\r
68                 return _timeout;\r
69             }\r
70             set {\r
71                 if (value < -1) {\r
72                     throw Error.AsyncCommon_InvalidTimeout("value");\r
73                 }\r
74                 _timeout = value;\r
75             }\r
76         }\r
77 \r
78     }\r
79 }\r