|
25 | 25 |
|
26 | 26 | namespace BytesRoad.Net.Sockets.Advanced |
27 | 27 | { |
28 | | - /// <summary> |
29 | | - /// Summary description for AsyncBase. |
30 | | - /// </summary> |
| 28 | + /// <summary> |
| 29 | + /// Summary description for AsyncBase. |
| 30 | + /// </summary> |
31 | 31 | public class AsyncBase |
32 | | - { |
33 | | - protected bool inProgress = false; |
| 32 | + { |
| 33 | + protected bool inProgress = false; |
34 | 34 | /* |
35 | | - internal AsyncBase() |
36 | | - { |
37 | | - } |
| 35 | + internal AsyncBase() |
| 36 | + { |
| 37 | + } |
38 | 38 | */ |
39 | | - |
40 | | - virtual internal void SetProgress(bool progress) |
41 | | - { |
42 | | - //prevent from nested calls |
43 | | - lock(this) |
44 | | - { |
45 | | - if(progress) |
46 | | - { |
47 | | - if(inProgress) |
48 | | - throw new InvalidOperationException("Attempt to start operation which is already in the progress"); |
49 | | - inProgress = true; |
50 | | - } |
51 | | - else |
52 | | - { |
53 | | - inProgress = false; |
54 | | - } |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - virtual internal void CheckProgress() |
59 | | - { |
60 | | - lock(this) |
61 | | - { |
62 | | - if(inProgress) |
63 | | - throw new InvalidOperationException("Attempt to start operation which is already in the progress"); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | -/* virtual internal void HandleAsyncEnd(IAsyncResult ar, Type arType, bool turnProgressOff) |
68 | | - { |
69 | | - HandleAsyncEnd(ar, arType, null, turnProgressOff); |
70 | | - }*/ |
71 | | - |
72 | | - static internal void VerifyAsyncResult(IAsyncResult ar, |
73 | | - Type arType) |
74 | | - { |
75 | | - VerifyAsyncResult(ar, arType, null); |
76 | | - } |
77 | | - |
78 | | - static internal void VerifyAsyncResult(IAsyncResult ar, |
79 | | - Type arType, |
80 | | - string metName) |
81 | | - { |
82 | | - if(null == ar) |
83 | | - throw new ArgumentNullException("asyncResult", "The value cannot be null."); |
84 | | - |
85 | | - if(null == metName) |
86 | | - metName = "End*"; |
87 | | - |
88 | | - if(false == ar.GetType().Equals(arType)) |
89 | | - throw new ArgumentException("asyncResult was not returned by a call to the " + |
90 | | - metName + " method.", "asyncResult"); |
91 | | - |
92 | | - AsyncResultBase stateObj = (AsyncResultBase)ar; |
93 | | - if(stateObj.IsHandled) |
94 | | - throw new InvalidOperationException(metName + " was previously called for the asynchronous operation."); |
95 | | - } |
96 | | - |
97 | | - virtual internal void HandleAsyncEnd(IAsyncResult ar, bool turnProgressOff) |
98 | | - { |
99 | | - if((false == ar.GetType().IsSubclassOf(typeof(AsyncResultBase))) && |
100 | | - (false == ar.GetType().Equals(typeof(AsyncResultBase)))) |
101 | | - throw new ArgumentException("asyncResult was not returned by a call to End* method.", "asyncResult"); |
102 | | - |
103 | | - AsyncResultBase stateObj = (AsyncResultBase)ar; |
104 | | - if(stateObj.IsHandled) |
105 | | - throw new InvalidOperationException("End* method was previously called for the asynchronous operation."); |
106 | | - |
107 | | - if(false == stateObj.IsCompleted) |
108 | | - stateObj.AsyncWaitHandle.WaitOne(); |
109 | | - |
110 | | - stateObj.IsHandled = true; |
111 | | - |
112 | | - if(turnProgressOff) |
113 | | - SetProgress(false); |
114 | | - |
115 | | - if(null != stateObj.Exception) |
116 | | - { |
117 | | - //dumpActivityException(stateObj); |
118 | | - throw stateObj.Exception; |
119 | | - } |
120 | | - } |
| 39 | + |
| 40 | + virtual internal void SetProgress(bool progress) |
| 41 | + { |
| 42 | + //prevent from nested calls |
| 43 | + lock(this) |
| 44 | + { |
| 45 | + if(progress) |
| 46 | + { |
| 47 | + if(inProgress) |
| 48 | + throw new InvalidOperationException("Attempt to start operation which is already in the progress"); |
| 49 | + inProgress = true; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + inProgress = false; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + virtual internal void CheckProgress() |
| 59 | + { |
| 60 | + lock(this) |
| 61 | + { |
| 62 | + if(inProgress) |
| 63 | + throw new InvalidOperationException("Attempt to start operation which is already in the progress"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +/* virtual internal void HandleAsyncEnd(IAsyncResult ar, Type arType, bool turnProgressOff) |
| 68 | + { |
| 69 | + HandleAsyncEnd(ar, arType, null, turnProgressOff); |
| 70 | + }*/ |
| 71 | + |
| 72 | + static internal void VerifyAsyncResult(IAsyncResult ar, |
| 73 | + Type arType) |
| 74 | + { |
| 75 | + VerifyAsyncResult(ar, arType, null); |
| 76 | + } |
| 77 | + |
| 78 | + static internal void VerifyAsyncResult(IAsyncResult ar, |
| 79 | + Type arType, |
| 80 | + string metName) |
| 81 | + { |
| 82 | + if(null == ar) |
| 83 | + throw new ArgumentNullException("asyncResult", "The value cannot be null."); |
| 84 | + |
| 85 | + if(null == metName) |
| 86 | + metName = "End*"; |
| 87 | + |
| 88 | + if(false == ar.GetType().Equals(arType)) |
| 89 | + throw new ArgumentException("asyncResult was not returned by a call to the " + |
| 90 | + metName + " method.", "asyncResult"); |
| 91 | + |
| 92 | + AsyncResultBase stateObj = (AsyncResultBase)ar; |
| 93 | + if(stateObj.IsHandled) |
| 94 | + throw new InvalidOperationException(metName + " was previously called for the asynchronous operation."); |
| 95 | + } |
| 96 | + |
| 97 | + virtual internal void HandleAsyncEnd(IAsyncResult ar, bool turnProgressOff) |
| 98 | + { |
| 99 | + if((false == ar.GetType().IsSubclassOf(typeof(AsyncResultBase))) && |
| 100 | + (false == ar.GetType().Equals(typeof(AsyncResultBase)))) |
| 101 | + throw new ArgumentException("asyncResult was not returned by a call to End* method.", "asyncResult"); |
| 102 | + |
| 103 | + AsyncResultBase stateObj = (AsyncResultBase)ar; |
| 104 | + if(stateObj.IsHandled) |
| 105 | + throw new InvalidOperationException("End* method was previously called for the asynchronous operation."); |
| 106 | + |
| 107 | + if(false == stateObj.IsCompleted) |
| 108 | + stateObj.AsyncWaitHandle.WaitOne(); |
| 109 | + |
| 110 | + stateObj.IsHandled = true; |
| 111 | + |
| 112 | + if(turnProgressOff) |
| 113 | + SetProgress(false); |
| 114 | + |
| 115 | + if(null != stateObj.Exception) |
| 116 | + { |
| 117 | + //dumpActivityException(stateObj); |
| 118 | + throw stateObj.Exception; |
| 119 | + } |
| 120 | + } |
121 | 121 | /* |
122 | | - void dumpActivityException(AsyncResultBase ar) |
123 | | - { |
124 | | - Exception e = ar.Exception; |
125 | | - int tid = Thread.CurrentThread.GetHashCode(); |
126 | | - string msg = string.Format("{0} -----------------------------", tid); |
127 | | - NSTrace.WriteLineError(msg); |
| 122 | + void dumpActivityException(AsyncResultBase ar) |
| 123 | + { |
| 124 | + Exception e = ar.Exception; |
| 125 | + int tid = Thread.CurrentThread.GetHashCode(); |
| 126 | + string msg = string.Format("{0} -----------------------------", tid); |
| 127 | + NSTrace.WriteLineError(msg); |
128 | 128 |
|
129 | | - msg = string.Format("{0} Activity: {1}", tid, ar.ActivityName); |
130 | | - NSTrace.WriteLineError(msg); |
| 129 | + msg = string.Format("{0} Activity: {1}", tid, ar.ActivityName); |
| 130 | + NSTrace.WriteLineError(msg); |
131 | 131 |
|
132 | | - msg = string.Format("{0} Exception: {1}", tid, e.GetType().FullName); |
133 | | - NSTrace.WriteLineError(msg); |
| 132 | + msg = string.Format("{0} Exception: {1}", tid, e.GetType().FullName); |
| 133 | + NSTrace.WriteLineError(msg); |
134 | 134 |
|
135 | | - msg = string.Format("{0} Message: {1}", tid, e.Message); |
136 | | - NSTrace.WriteLineError(msg); |
| 135 | + msg = string.Format("{0} Message: {1}", tid, e.Message); |
| 136 | + NSTrace.WriteLineError(msg); |
137 | 137 |
|
138 | | - msg = string.Format("{0} Stack: {1}", tid, e.StackTrace); |
139 | | - NSTrace.WriteLineError(msg); |
| 138 | + msg = string.Format("{0} Stack: {1}", tid, e.StackTrace); |
| 139 | + NSTrace.WriteLineError(msg); |
140 | 140 |
|
141 | | - msg = string.Format("{0} -----------------------------", tid); |
142 | | - NSTrace.WriteLineError(msg); |
143 | | - } |
| 141 | + msg = string.Format("{0} -----------------------------", tid); |
| 142 | + NSTrace.WriteLineError(msg); |
| 143 | + } |
144 | 144 | */ |
145 | | - } |
| 145 | + } |
146 | 146 | } |
0 commit comments