This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientAsyncResult.cs
1 // \r
2 // System.Web.Services.Protocols.WebClientAsyncResult.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //   Lluis Sanchez Gual (lluis@ximian.com)\r
7 //\r
8 // Copyright (C) Tim Coleman, 2002\r
9 //\r
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 \r
32 using System.Net;\r
33 using System.Threading;\r
34 \r
35 namespace System.Web.Services.Protocols {\r
36         public class WebClientAsyncResult : IAsyncResult {\r
37 \r
38                 #region Fields\r
39 \r
40                 AsyncCallback _callback;\r
41                 object _asyncState;\r
42 \r
43                 bool _completedSynchronously;\r
44                 bool _done;\r
45                 ManualResetEvent _waitHandle;\r
46                 \r
47                 internal object Result;\r
48                 internal Exception Exception;\r
49                 internal WebRequest Request;\r
50                         \r
51                 #endregion // Fields\r
52 \r
53                 #region Constructors \r
54 \r
55                 internal WebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)\r
56                 {\r
57                         _callback = callback; \r
58                         Request = request;\r
59                         _asyncState = asyncState;\r
60                 }\r
61 \r
62                 #endregion // Constructors\r
63 \r
64                 #region Properties\r
65 \r
66                 public object AsyncState {\r
67                         get { return _asyncState; }\r
68                 }\r
69 \r
70                 public WaitHandle AsyncWaitHandle \r
71                 {\r
72                         get\r
73                         {\r
74                                 lock (this) {\r
75                                         if (_waitHandle != null) return _waitHandle;\r
76                                         _waitHandle = new ManualResetEvent (_done);\r
77                                         return _waitHandle;\r
78                                 }\r
79                         }\r
80                 }\r
81 \r
82                 public bool CompletedSynchronously \r
83                 {\r
84                         get { return _completedSynchronously; }\r
85                 }\r
86 \r
87                 public bool IsCompleted \r
88                 {\r
89                         get { lock (this) { return _done; } }\r
90                 }\r
91 \r
92                 #endregion // Properties\r
93 \r
94                 #region Methods\r
95 \r
96                 public void Abort ()\r
97                 {\r
98                         Request.Abort ();\r
99                 }\r
100 \r
101                 internal void SetCompleted (object result, Exception exception, bool async)\r
102                 {\r
103                         lock (this)\r
104                         {\r
105                                 Exception = exception;\r
106                                 Result = result;\r
107                                 _done = true;\r
108                                 _completedSynchronously = async;\r
109                                 if (_waitHandle != null) _waitHandle.Set ();\r
110                                 Monitor.PulseAll (this);\r
111                         }\r
112                         if (_callback != null) _callback (this);\r
113                 }\r
114 \r
115                 internal void WaitForComplete ()\r
116                 {\r
117                         lock (this)\r
118                         {\r
119                                 Monitor.Wait (this);\r
120                         }\r
121                 }\r
122 \r
123                 #endregion // Methods\r
124         }\r
125 }