Merge pull request #2408 from tastywheattasteslikechicken/MoreInterfaceSupport
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds.Protocol / TdsAsyncResult.cs
1 //
2 // Mono.Data.Tds.Protocol.TdsAsyncResult.cs
3 //
4 // Author:
5 //   T Sureshkumar <tsureshkumar@novell.com>
6 //   Ankit Jain <radical@corewars.org>
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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
32
33 using System;
34 using System.Threading;
35
36 namespace Mono.Data.Tds.Protocol
37 {
38         internal class TdsAsyncResult : IAsyncResult
39         {
40                 private TdsAsyncState _tdsState;
41                 private WaitHandle _waitHandle;
42                 private bool _completed = false;
43                 private bool _completedSyncly = false;
44                 private AsyncCallback _userCallback;
45                 private object _retValue;
46                 private Exception _exception = null;
47
48                 public TdsAsyncResult (AsyncCallback userCallback, TdsAsyncState tdsState)
49                 {
50                         _tdsState = tdsState;
51                         _userCallback = userCallback;
52                         _waitHandle = new ManualResetEvent (false);
53                 }
54
55                 public TdsAsyncResult (AsyncCallback userCallback, object state)
56                 {
57                         _tdsState = new TdsAsyncState (state);
58                         _userCallback = userCallback;
59                         _waitHandle = new ManualResetEvent (false);
60                 }
61                 
62
63                 public object AsyncState 
64                 {
65                         get {   return _tdsState.UserState; }
66                 }
67
68                 internal TdsAsyncState TdsAsyncState
69                 {
70                         get {   return _tdsState; }
71                 }
72
73                 public WaitHandle AsyncWaitHandle 
74                 {
75                         get { return _waitHandle; }
76
77                 }
78
79                 public bool IsCompleted 
80                 {
81                         get {   return _completed; }
82                 }
83
84                 public bool IsCompletedWithException
85                 {
86                         get {   return _exception != null; }
87                 }
88
89                 public Exception Exception
90                 {
91                         get {   return _exception; }
92                 }
93
94                 public bool CompletedSynchronously 
95                 {
96                         get {   return _completedSyncly; }
97                 }
98
99                 internal object ReturnValue
100                 {
101                         get {   return _retValue; }
102                         set {   _retValue = value; }
103                 }
104
105                 internal void MarkComplete () 
106                 {
107                         _completed = true;
108                         _exception = null;
109                         ((ManualResetEvent)_waitHandle).Set ();
110
111                         if (_userCallback != null)
112                                 _userCallback (this);
113                 }
114
115                 internal void MarkComplete (Exception e) 
116                 {
117                         _completed = true;
118                         _exception = e;
119                         ((ManualResetEvent)_waitHandle).Set ();
120
121                         if (_userCallback != null)
122                                 _userCallback (this);
123                 }
124         }
125 }
126