UpdateClient.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.ComponentModel;
  3. using System.Net;
  4. using System.Threading;
  5. using Update.Net.Events;
  6. namespace Update.Net
  7. {
  8. /// <summary>
  9. /// 异步操作网络客户端 by 许崇雷
  10. /// </summary>
  11. [ToolboxItem(false)]
  12. public partial class UpdateClient : WebClient
  13. {
  14. private bool m_InitAsync; //是否已初始化异步操作
  15. private bool m_Cancelled; //是否取消异步操作
  16. private int m_CallNesting; //如果大于 0 表示有异步工作正在进行
  17. private AsyncOperation m_AsyncOp; //异步操作生存期
  18. private ProgressData m_Progress; //异步操作进度数据
  19. #region 属性
  20. /// <summary>
  21. /// 获取是否正在进行异步操作
  22. /// </summary>
  23. public new virtual bool IsBusy
  24. {
  25. get
  26. {
  27. return base.IsBusy || this.m_AsyncOp != null;
  28. }
  29. }
  30. #endregion
  31. #region 状态
  32. /// <summary>
  33. /// 初始化异步操作
  34. /// </summary>
  35. private void InitAsync()
  36. {
  37. if (this.m_InitAsync)
  38. return;
  39. this.m_Progress = new ProgressData();
  40. this.m_KillProcessStart = new KillProcessStartDelegate(this.KillProcessWork);
  41. this.m_ReportKillProgressChanged = new SendOrPostCallback(this.ReportKillProgressChanged);
  42. this.m_KillProcessOperationCompleted = new SendOrPostCallback(this.KillProcessOperationCompleted);
  43. this.m_DecompressDataStart = new DecompressDataStartDelegate(this.DecompressDataWork);
  44. this.m_ReportDecompressProgressChanged = new SendOrPostCallback(this.ReportDecompressProgressChanged);
  45. this.m_DecompressDataOperationCompleted = new SendOrPostCallback(this.DecompressDataOperationCompleted);
  46. this.m_InitAsync = true;
  47. }
  48. /// <summary>
  49. /// 清空状态
  50. /// </summary>
  51. private void ClearState()
  52. {
  53. if (this.AnotherCallInProgress(Interlocked.Increment(ref this.m_CallNesting)))
  54. {
  55. this.CompleteState();
  56. throw new NotSupportedException("无效的操作,操作占用中。");
  57. }
  58. this.m_Cancelled = false;
  59. if (this.m_Progress != null)
  60. this.m_Progress.Reset();
  61. }
  62. /// <summary>
  63. /// 完成状态
  64. /// </summary>
  65. private void CompleteState()
  66. {
  67. Interlocked.Decrement(ref m_CallNesting);
  68. }
  69. /// <summary>
  70. /// 是否有其他异步操作正在进行
  71. /// </summary>
  72. /// <param name="callNesting">计数</param>
  73. /// <returns>有返回true,否则返回false</returns>
  74. private bool AnotherCallInProgress(int callNesting)
  75. {
  76. return callNesting > 1;
  77. }
  78. #endregion
  79. #region 执行回调
  80. /// <summary>
  81. /// 往异步开始线程发送回调
  82. /// </summary>
  83. /// <param name="asyncOp">异步生存期</param>
  84. /// <param name="callback">要发送的委托</param>
  85. /// <param name="eventArgs">数据</param>
  86. private void InvokeOperation(AsyncOperation asyncOp, SendOrPostCallback callback, object eventArgs)
  87. {
  88. asyncOp.Post(callback, eventArgs);
  89. }
  90. /// <summary>
  91. /// 往异步开始线程发送完成
  92. /// </summary>
  93. /// <param name="asyncOp">异步生存期</param>
  94. /// <param name="callback">要发送的委托</param>
  95. /// <param name="eventArgs">数据</param>
  96. private void InvokeOperationCompleted(AsyncOperation asyncOp, SendOrPostCallback callback, AsyncCompletedEventArgs eventArgs)
  97. {
  98. if (Interlocked.CompareExchange(ref this.m_AsyncOp, null, asyncOp) == asyncOp)
  99. {
  100. this.CompleteState();
  101. asyncOp.PostOperationCompleted(callback, eventArgs);
  102. }
  103. }
  104. #endregion
  105. #region 公共方法
  106. /// <summary>
  107. /// 取消异步操作
  108. /// </summary>
  109. public new virtual void CancelAsync()
  110. {
  111. this.m_Cancelled = true;
  112. base.CancelAsync();
  113. }
  114. #endregion
  115. }
  116. }