2 // System.Net.WebAsyncResult
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Martin Baulig (martin.baulig@xamarin.com)
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // Copyright (c) 2014 Xamarin Inc. (http://www.xamarin.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Threading;
37 delegate void SimpleAsyncCallback (SimpleAsyncResult result);
39 delegate bool SimpleAsyncFunc (SimpleAsyncResult result);
41 class SimpleAsyncResult : IAsyncResult
43 ManualResetEvent handle;
46 SimpleAsyncCallback cb;
50 object locker = new object ();
52 SimpleAsyncResult (SimpleAsyncCallback cb)
57 protected SimpleAsyncResult (AsyncCallback cb, object state)
66 public static SimpleAsyncResult Run (SimpleAsyncFunc func, SimpleAsyncCallback callback)
68 var result = new SimpleAsyncResult (callback);
71 result.SetCompleted (true);
72 } catch (Exception ex) {
73 result.SetCompleted (true, ex);
78 public static SimpleAsyncResult RunWithLock (object locker, SimpleAsyncFunc func, SimpleAsyncCallback callback)
80 return Run (inner => {
81 bool running = func (inner);
83 Monitor.Exit (locker);
86 if (inner.GotException) {
87 if (inner.CompletedSynchronously)
88 Monitor.Exit (locker);
94 if (!inner.CompletedSynchronously)
95 Monitor.Enter (locker);
99 Monitor.Exit (locker);
104 protected void Reset_internal ()
106 callbackDone = false;
116 internal void SetCompleted (bool synch, Exception e)
118 SetCompleted_internal (synch, e);
119 DoCallback_private ();
122 internal void SetCompleted (bool synch)
124 SetCompleted_internal (synch);
125 DoCallback_private ();
128 protected void SetCompleted_internal (bool synch, Exception e)
139 protected void SetCompleted_internal (bool synch)
150 void DoCallback_private ()
153 throw new InvalidOperationException ();
160 protected void DoCallback_internal ()
162 if (!callbackDone && cb != null) {
167 ThreadPool.QueueUserWorkItem (CB, null);
171 void CB (object unused)
176 internal void WaitUntilComplete ()
181 AsyncWaitHandle.WaitOne ();
184 internal bool WaitUntilComplete (int timeout, bool exitContext)
189 return AsyncWaitHandle.WaitOne (timeout, exitContext);
192 public object AsyncState {
193 get { return state; }
196 public WaitHandle AsyncWaitHandle {
200 handle = new ManualResetEvent (isCompleted);
207 public bool CompletedSynchronously {
208 get { return synch; }
211 public bool IsCompleted {
219 internal bool GotException {
220 get { return (exc != null); }
223 internal Exception Exception {