Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / System.Transactions / System.Transactions / CommittableTransaction.cs
1 //
2 // CommittableTransaction.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //      Ankit Jain       <JAnkit@novell.com>
7 //
8 // (C)2005 Novell Inc,
9 // (C)2006 Novell Inc,
10 //
11
12 using System.Runtime.Serialization;
13 using System.Threading;
14
15 namespace System.Transactions
16 {
17         [Serializable]
18         public sealed class CommittableTransaction : Transaction,
19                 ISerializable, IDisposable, System.IAsyncResult
20         {
21                 TransactionOptions options;
22
23                 AsyncCallback callback;
24                 object user_defined_state;
25
26                 IAsyncResult asyncResult;
27
28                 public CommittableTransaction ()
29                         : this (new TransactionOptions ())
30                 {
31                 }
32
33                 public CommittableTransaction (TimeSpan timeout)
34                 {
35                         options = new TransactionOptions ();
36                         options.Timeout = timeout;
37                 }
38
39                 public CommittableTransaction (TransactionOptions options)
40                 {
41                         this.options = options;
42                 }
43
44                 public IAsyncResult BeginCommit (AsyncCallback asyncCallback,
45                         object asyncState)
46                 {
47                         this.callback = asyncCallback;
48                         this.user_defined_state = asyncState;
49
50                         AsyncCallback cb = null;
51                         if (asyncCallback != null)
52                                 cb = new AsyncCallback (CommitCallback);
53
54                         asyncResult = BeginCommitInternal (cb);
55                         return this;
56                 }
57                 
58                 public void EndCommit (IAsyncResult asyncResult)
59                 {
60                         if (asyncResult != this)
61                                 throw new ArgumentException ("The IAsyncResult parameter must be the same parameter as returned by BeginCommit.", "asyncResult");
62
63                         EndCommitInternal (this.asyncResult);
64                 }
65
66                 private void CommitCallback (IAsyncResult ar)
67                 {
68                         if (asyncResult == null && ar.CompletedSynchronously)
69                                 asyncResult = ar;
70                         callback (this);
71                 }
72
73                 public void Commit ()
74                 {
75                         CommitInternal ();
76                 }
77                 
78                 [MonoTODO ("Not implemented")]
79                 void ISerializable.GetObjectData (SerializationInfo info,
80                         StreamingContext context)
81                 {
82                         throw new NotImplementedException ();
83                 }
84
85                 object IAsyncResult.AsyncState {
86                         get { return user_defined_state; }
87                 }
88
89                 WaitHandle IAsyncResult.AsyncWaitHandle {
90                         get { return asyncResult.AsyncWaitHandle; }
91                 }
92
93                 bool IAsyncResult.CompletedSynchronously {
94                         get { return asyncResult.CompletedSynchronously; }
95                 }
96
97                 bool IAsyncResult.IsCompleted {
98                         get { return asyncResult.IsCompleted; }
99                 }
100
101
102         }
103 }
104