AsyncResult.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace Mono.Nat
  6. {
  7. internal class AsyncResult : IAsyncResult
  8. {
  9. private object asyncState;
  10. private AsyncCallback callback;
  11. private bool completedSynchronously;
  12. private bool isCompleted;
  13. private Exception storedException;
  14. private ManualResetEvent waitHandle;
  15. public AsyncResult(AsyncCallback callback, object asyncState)
  16. {
  17. this.callback = callback;
  18. this.asyncState = asyncState;
  19. waitHandle = new ManualResetEvent(false);
  20. }
  21. public object AsyncState
  22. {
  23. get { return asyncState; }
  24. }
  25. public ManualResetEvent AsyncWaitHandle
  26. {
  27. get { return waitHandle; }
  28. }
  29. WaitHandle IAsyncResult.AsyncWaitHandle
  30. {
  31. get { return waitHandle; }
  32. }
  33. public bool CompletedSynchronously
  34. {
  35. get { return completedSynchronously; }
  36. protected internal set { completedSynchronously = value; }
  37. }
  38. public bool IsCompleted
  39. {
  40. get { return isCompleted; }
  41. protected internal set { isCompleted = value; }
  42. }
  43. public Exception StoredException
  44. {
  45. get { return storedException; }
  46. }
  47. public void Complete()
  48. {
  49. Complete(storedException);
  50. }
  51. public void Complete(Exception ex)
  52. {
  53. storedException = ex;
  54. isCompleted = true;
  55. waitHandle.Set();
  56. if (callback != null)
  57. callback(this);
  58. }
  59. }
  60. }