Mercurial > hg > ucis.core
comparison Util/AsyncResultBase.cs @ 5:2933f7750542
Added read-buffering stream wrapper
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 08 Jan 2013 16:38:21 +0100 |
parents | |
children | 4b78cc5f116b |
comparison
equal
deleted
inserted
replaced
4:9e2e6433f57a | 5:2933f7750542 |
---|---|
1 using System; | |
2 using System.Threading; | |
3 using SysThreadPool = System.Threading.ThreadPool; | |
4 | |
5 namespace UCIS.Util { | |
6 public abstract class AsyncResultBase : IAsyncResult { | |
7 ManualResetEvent WaitEvent = null; | |
8 AsyncCallback Callback = null; | |
9 public object AsyncState { get; private set; } | |
10 public bool CompletedSynchronously { get; private set; } | |
11 public bool IsCompleted { get; private set; } | |
12 public Exception Error { get; private set; } | |
13 public WaitHandle AsyncWaitHandle { | |
14 get { | |
15 lock (this) { | |
16 if (WaitEvent == null) WaitEvent = new ManualResetEvent(IsCompleted); | |
17 return WaitEvent; | |
18 } | |
19 } | |
20 } | |
21 | |
22 public AsyncResultBase(AsyncCallback callback, Object state) { | |
23 this.Callback = callback; | |
24 this.AsyncState = state; | |
25 } | |
26 | |
27 private void CallCallback(Object state) { | |
28 if (Callback != null) Callback(this); | |
29 } | |
30 | |
31 protected void SetCompleted(Boolean synchronously, Exception error) { | |
32 this.CompletedSynchronously = synchronously; | |
33 this.Error = error; | |
34 lock (this) { | |
35 IsCompleted = true; | |
36 if (WaitEvent != null) WaitEvent.Set(); | |
37 } | |
38 if (Callback != null) SysThreadPool.QueueUserWorkItem(CallCallback); | |
39 } | |
40 | |
41 protected void ThrowError() { | |
42 if (Error != null) throw Error; | |
43 } | |
44 } | |
45 } |