WebClientAsyncResult.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Net;
  31. using System.Threading;
  32. namespace System.Web.Services.Protocols {
  33. public class WebClientAsyncResult : IAsyncResult {
  34. #region Fields
  35. AsyncCallback _callback;
  36. object _asyncState;
  37. bool _completedSynchronously;
  38. bool _done;
  39. ManualResetEvent _waitHandle;
  40. internal object Result;
  41. internal Exception Exception;
  42. internal WebRequest Request;
  43. #endregion // Fields
  44. #region Constructors
  45. internal WebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
  46. {
  47. _callback = callback;
  48. Request = request;
  49. _asyncState = asyncState;
  50. }
  51. #endregion // Constructors
  52. #region Properties
  53. public object AsyncState {
  54. get { return _asyncState; }
  55. }
  56. public WaitHandle AsyncWaitHandle
  57. {
  58. get
  59. {
  60. lock (this) {
  61. if (_waitHandle != null) return _waitHandle;
  62. _waitHandle = new ManualResetEvent (_done);
  63. return _waitHandle;
  64. }
  65. }
  66. }
  67. public bool CompletedSynchronously
  68. {
  69. get { return _completedSynchronously; }
  70. }
  71. public bool IsCompleted
  72. {
  73. get { lock (this) { return _done; } }
  74. }
  75. #endregion // Properties
  76. #region Methods
  77. public void Abort ()
  78. {
  79. Request.Abort ();
  80. }
  81. internal void SetCompleted (object result, Exception exception, bool async)
  82. {
  83. lock (this)
  84. {
  85. Exception = exception;
  86. Result = result;
  87. _done = true;
  88. _completedSynchronously = async;
  89. if (_waitHandle != null) _waitHandle.Set ();
  90. Monitor.PulseAll (this);
  91. }
  92. if (_callback != null) _callback (this);
  93. }
  94. internal void WaitForComplete ()
  95. {
  96. lock (this)
  97. {
  98. Monitor.Wait (this);
  99. }
  100. }
  101. #endregion // Methods
  102. }
  103. }