WebClientAsyncResult.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // System.Web.Services.Protocols.WebClientAsyncResult.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. using System.Net;
  11. using System.Threading;
  12. namespace System.Web.Services.Protocols {
  13. public class WebClientAsyncResult : IAsyncResult {
  14. #region Fields
  15. AsyncCallback _callback;
  16. object _asyncState;
  17. bool _completedSynchronously;
  18. bool _done;
  19. ManualResetEvent _waitHandle;
  20. internal object Result;
  21. internal Exception Exception;
  22. internal WebRequest Request;
  23. #endregion // Fields
  24. #region Constructors
  25. internal WebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
  26. {
  27. _callback = callback;
  28. Request = request;
  29. _asyncState = asyncState;
  30. }
  31. #endregion // Constructors
  32. #region Properties
  33. public object AsyncState {
  34. get { return _asyncState; }
  35. }
  36. public WaitHandle AsyncWaitHandle
  37. {
  38. get
  39. {
  40. lock (this) {
  41. if (_waitHandle != null) return _waitHandle;
  42. _waitHandle = new ManualResetEvent (_done);
  43. return _waitHandle;
  44. }
  45. }
  46. }
  47. public bool CompletedSynchronously
  48. {
  49. get { return _completedSynchronously; }
  50. }
  51. public bool IsCompleted
  52. {
  53. get { lock (this) { return _done; } }
  54. }
  55. #endregion // Properties
  56. #region Methods
  57. public void Abort ()
  58. {
  59. Request.Abort ();
  60. }
  61. internal void SetCompleted (object result, Exception exception, bool async)
  62. {
  63. lock (this)
  64. {
  65. Exception = exception;
  66. Result = result;
  67. _done = true;
  68. _completedSynchronously = async;
  69. if (_waitHandle != null) _waitHandle.Set ();
  70. Monitor.PulseAll (this);
  71. }
  72. if (_callback != null) _callback (this);
  73. }
  74. internal void WaitForComplete ()
  75. {
  76. lock (this)
  77. {
  78. Monitor.Wait (this);
  79. }
  80. }
  81. #endregion // Methods
  82. }
  83. }