2 // Microsoft.Web.Services.AsyncResult.cs
4 // Author: Todd Berman <tberman@gentoo.org>
6 // (C) 2003 Todd Berman
9 using System.Threading;
11 namespace Microsoft.Web.Services
13 public class AsyncResult : IAsyncResult
15 private AsyncCallback _callback;
16 private bool _completed;
17 private bool _completedSync;
18 private bool _endCalled;
19 private ManualResetEvent _event;
20 private Exception _exception;
21 private object _state;
23 protected AsyncResult (object s) : this (null, s)
27 protected AsyncResult (AsyncCallback call, object s) : base ()
33 protected AsyncResult () : this (null, null)
37 protected void Complete (bool csync, Exception e)
40 _completedSync = csync;
47 if(_callback != null) {
51 ThreadPool.QueueUserWorkItem (new WaitCallback (ThrowException), this);
55 protected void Complete (bool csync)
57 this.Complete (csync, null);
60 public static void End (IAsyncResult result)
63 throw new ArgumentNullException ("result");
65 AsyncResult mws_result = (AsyncResult) result;
67 if(mws_result == null) {
68 throw new ArgumentException ("Invalid result");
71 if(mws_result._endCalled == true) {
72 throw new InvalidOperationException ("Async Operation already finished");
75 mws_result._endCalled = true;
77 if(mws_result._completed == true) {
78 mws_result.AsyncWaitHandle.WaitOne ();
81 if(mws_result._exception != null) {
82 throw mws_result._exception;
86 private void ThrowException (object o)
88 Exception e = (Exception) o;
92 public object AsyncState {
93 get { return _state; }
96 public WaitHandle AsyncWaitHandle {
99 bool complete = _completed;
102 _event = new ManualResetEvent (_completed);
104 if(complete == true || _completed == false) {
112 public bool CompletedSynchronously {
113 get { return _completedSync; }
116 public bool IsCompleted {
117 get { return _completed; }