1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Async {
\r
15 using System.Collections.Generic;
\r
16 using System.Threading;
\r
18 public class AsyncManager {
\r
20 private readonly SynchronizationContext _syncContext;
\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
26 public AsyncManager()
\r
27 : this(null /* syncContext */) {
\r
30 public AsyncManager(SynchronizationContext syncContext) {
\r
31 _syncContext = syncContext ?? SynchronizationContextUtil.GetSynchronizationContext();
\r
33 OutstandingOperations = new OperationCounter();
\r
34 OutstandingOperations.Completed += delegate { Finish(); };
\r
36 Parameters = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
\r
39 public OperationCounter OutstandingOperations {
\r
44 public IDictionary<string, object> Parameters {
\r
49 public event EventHandler Finished;
\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
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
65 // measured in milliseconds, Timeout.Infinite means 'no timeout'
\r
66 public int Timeout {
\r
72 throw Error.AsyncCommon_InvalidTimeout("value");
\r