AsyncResult.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Threading;
  3. namespace Muchinfo.MTPClient.Infrastructure.LinkProxy
  4. {
  5. /// <summary>
  6. /// 异步结果
  7. /// </summary>
  8. /// <typeparam name="T"></typeparam>
  9. public class AsyncResult<T> : IAsyncResult // where T : Tas.BusinessManager.DataContract.ResponseBase
  10. {
  11. public AsyncResult()
  12. {
  13. this.AsyncWaitHandle = new ManualResetEvent(false);
  14. this.RequestTime = DateTime.Now;
  15. }
  16. public AsyncResult(DateTime reqeustTime)
  17. {
  18. this.AsyncWaitHandle = new ManualResetEvent(false);
  19. this.RequestTime = reqeustTime;
  20. }
  21. /// <summary>
  22. /// 设置完成
  23. /// </summary>
  24. /// <param name="result">The result.</param>
  25. /// <param name="responseTime">The response time.</param>
  26. public void Complete(T result, DateTime responseTime)
  27. {
  28. this.ResponseTime = responseTime;
  29. this.Result = result;
  30. this.IsCompleted = true;
  31. var manualReset = (this.AsyncWaitHandle as ManualResetEvent);
  32. if (manualReset == null)
  33. {
  34. return;
  35. }
  36. manualReset.Set();
  37. }
  38. /// <summary>
  39. /// 设置服务超时
  40. /// </summary>
  41. public void SetTimeOut()
  42. {
  43. this.IsCompleted = false;
  44. var manualReset = (this.AsyncWaitHandle as ManualResetEvent);
  45. if (manualReset == null)
  46. {
  47. return;
  48. }
  49. manualReset.Set();
  50. }
  51. /// <summary>
  52. /// 返回结果
  53. /// </summary>
  54. public T Result
  55. {
  56. get;
  57. private set;
  58. }
  59. /// <summary>
  60. /// 请求时间
  61. /// </summary>
  62. public DateTime RequestTime
  63. {
  64. get;
  65. private set;
  66. }
  67. /// <summary>
  68. /// 响应时间
  69. /// </summary>
  70. public DateTime ResponseTime
  71. {
  72. get;
  73. private set;
  74. }
  75. public WaitHandle AsyncWaitHandle
  76. {
  77. get;
  78. private set;
  79. }
  80. public bool CompletedSynchronously
  81. {
  82. get
  83. {
  84. return false;
  85. }
  86. }
  87. public object AsyncState
  88. {
  89. get;
  90. private set;
  91. }
  92. public bool IsCompleted
  93. {
  94. get;
  95. private set;
  96. }
  97. }
  98. }