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 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 void Run (SimpleAsyncFunc func, SimpleAsyncCallback callback)
68 var result = new SimpleAsyncResult (callback);
71 result.SetCompleted (true);
72 } catch (Exception ex) {
73 result.SetCompleted (true, ex);
77 public static void RunWithLock (object locker, SimpleAsyncFunc func, SimpleAsyncCallback callback)
80 bool running = func (inner);
82 Monitor.Exit (locker);
85 if (inner.GotException) {
86 if (inner.CompletedSynchronously)
87 Monitor.Exit (locker);
93 if (!inner.CompletedSynchronously)
94 Monitor.Enter (locker);
98 Monitor.Exit (locker);
103 protected void Reset_internal ()
105 callbackDone = false;
114 internal void SetCompleted (bool synch, Exception e)
116 SetCompleted_internal (synch, e);
117 DoCallback_private ();
120 internal void SetCompleted (bool synch)
122 SetCompleted_internal (synch);
123 DoCallback_private ();
126 protected void SetCompleted_internal (bool synch, Exception e)
137 protected void SetCompleted_internal (bool synch)
148 void DoCallback_private ()
158 protected void DoCallback_internal ()
160 if (!callbackDone && cb != null) {
166 internal void WaitUntilComplete ()
171 AsyncWaitHandle.WaitOne ();
174 internal bool WaitUntilComplete (int timeout, bool exitContext)
179 return AsyncWaitHandle.WaitOne (timeout, exitContext);
182 public object AsyncState {
183 get { return state; }
186 public WaitHandle AsyncWaitHandle {
190 handle = new ManualResetEvent (isCompleted);
197 public bool CompletedSynchronously {
198 get { return synch; }
201 public bool IsCompleted {
209 internal bool GotException {
210 get { return (exc != null); }
213 internal Exception Exception {