2 // SimpleAsyncResult.cs
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 class SimpleAsyncResult : IAsyncResult
41 ManualResetEvent handle;
44 readonly SimpleAsyncCallback cb;
48 object locker = new object ();
50 SimpleAsyncResult (SimpleAsyncCallback cb)
55 protected SimpleAsyncResult (AsyncCallback cb, object state)
64 public static void Run (Func<SimpleAsyncResult, bool> func, SimpleAsyncCallback callback)
66 var result = new SimpleAsyncResult (callback);
69 result.SetCompleted (true);
70 } catch (Exception ex) {
71 result.SetCompleted (true, ex);
75 public static void RunWithLock (object locker, Func<SimpleAsyncResult, bool> func, SimpleAsyncCallback callback)
78 bool running = func (inner);
80 Monitor.Exit (locker);
83 if (inner.GotException) {
84 if (inner.CompletedSynchronously)
85 Monitor.Exit (locker);
91 if (!inner.CompletedSynchronously)
92 Monitor.Enter (locker);
96 Monitor.Exit (locker);
101 protected void Reset_internal ()
103 callbackDone = false;
112 internal void SetCompleted (bool synch, Exception e)
114 SetCompleted_internal (synch, e);
115 DoCallback_private ();
118 internal void SetCompleted (bool synch)
120 SetCompleted_internal (synch);
121 DoCallback_private ();
124 void SetCompleted_internal (bool synch, Exception e)
135 protected void SetCompleted_internal (bool synch)
137 SetCompleted_internal (synch, null);
140 void DoCallback_private ()
150 protected void DoCallback_internal ()
152 if (!callbackDone && cb != null) {
158 internal void WaitUntilComplete ()
163 AsyncWaitHandle.WaitOne ();
166 internal bool WaitUntilComplete (int timeout, bool exitContext)
171 return AsyncWaitHandle.WaitOne (timeout, exitContext);
174 public object AsyncState {
175 get { return state; }
178 public WaitHandle AsyncWaitHandle {
182 handle = new ManualResetEvent (isCompleted);
189 public bool CompletedSynchronously {
190 get { return synch; }
193 public bool IsCompleted {
201 internal bool GotException {
202 get { return (exc != null); }
205 internal Exception Exception {