UpdateClient.KillProcess.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using Update.Net.Events;
  6. namespace Update.Net
  7. {
  8. partial class UpdateClient
  9. {
  10. private KillProcessStartDelegate m_KillProcessStart; //异步结束进程委托
  11. private SendOrPostCallback m_ReportKillProgressChanged; //异步结束进程报告回调
  12. private SendOrPostCallback m_KillProcessOperationCompleted; //异步结束进程完成回调
  13. #region 事件
  14. //结束进程进度
  15. private static readonly object EVENT_KILL_PROGRESS_CHANGED = new object();
  16. /// <summary>
  17. /// 结束进程进度事件
  18. /// </summary>
  19. public event KillProgressChangedEventHandler KillProgressChanged
  20. {
  21. add { this.Events.AddHandler(EVENT_KILL_PROGRESS_CHANGED, value); }
  22. remove { this.Events.RemoveHandler(EVENT_KILL_PROGRESS_CHANGED, value); }
  23. }
  24. /// <summary>
  25. /// 触发结束进程进度事件
  26. /// </summary>
  27. /// <param name="e">数据</param>
  28. protected virtual void OnKillProgressChanged(KillProgressChangedEventArgs e)
  29. {
  30. KillProgressChangedEventHandler handler = this.Events[EVENT_KILL_PROGRESS_CHANGED] as KillProgressChangedEventHandler;
  31. if (handler != null)
  32. handler(this, e);
  33. }
  34. /// <summary>
  35. /// 触发结束进程进度事件
  36. /// </summary>
  37. /// <param name="state">数据</param>
  38. private void ReportKillProgressChanged(object state)
  39. {
  40. this.OnKillProgressChanged(state as KillProgressChangedEventArgs);
  41. }
  42. //结束进程完成
  43. private static readonly object EVENT_KILL_PROCESS_COMPLETED = new object();
  44. /// <summary>
  45. /// 结束进程完成事件
  46. /// </summary>
  47. public event KillProcessCompletedEventHandler KillProcessCompleted
  48. {
  49. add { this.Events.AddHandler(EVENT_KILL_PROCESS_COMPLETED, value); }
  50. remove { this.Events.RemoveHandler(EVENT_KILL_PROCESS_COMPLETED, value); }
  51. }
  52. /// <summary>
  53. /// 触发结束进程完成事件
  54. /// </summary>
  55. /// <param name="e">数据</param>
  56. protected virtual void OnKillProcessCompleted(KillProcessCompletedEventArgs e)
  57. {
  58. KillProcessCompletedEventHandler handler = this.Events[EVENT_KILL_PROCESS_COMPLETED] as KillProcessCompletedEventHandler;
  59. if (handler != null)
  60. handler(this, e);
  61. }
  62. /// <summary>
  63. /// 触发结束进程完成事件
  64. /// </summary>
  65. /// <param name="state">数据</param>
  66. private void KillProcessOperationCompleted(object state)
  67. {
  68. this.OnKillProcessCompleted(state as KillProcessCompletedEventArgs);
  69. }
  70. #endregion
  71. #region 执行回调
  72. /// <summary>
  73. /// 执行结束进度报告回调
  74. /// </summary>
  75. /// <param name="progress">进度数据</param>
  76. /// <param name="asyncOp">异步生存期</param>
  77. private void PostKillProgressChanged(ProgressData progress, AsyncOperation asyncOp)
  78. {
  79. if (asyncOp == null)
  80. return;
  81. int progressPercentage = progress.ToComplete < 0L ? 0 : (progress.ToComplete == 0L ? 100 : (int)(100L * progress.Completed / progress.ToComplete));
  82. KillProgressChangedEventArgs eventArgs = new KillProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
  83. this.InvokeOperation(asyncOp, this.m_ReportKillProgressChanged, eventArgs);
  84. }
  85. /// <summary>
  86. /// 执行结束进程完成回调
  87. /// </summary>
  88. /// <param name="error">错误</param>
  89. /// <param name="cancelled">是否取消</param>
  90. /// <param name="asyncOp">异步生存期</param>
  91. private void KillProcessAsyncCallback(Exception error, bool cancelled, AsyncOperation asyncOp)
  92. {
  93. KillProcessCompletedEventArgs eventArgs = new KillProcessCompletedEventArgs(error, cancelled, asyncOp.UserSuppliedState);
  94. this.InvokeOperationCompleted(asyncOp, this.m_KillProcessOperationCompleted, eventArgs);
  95. }
  96. #endregion
  97. #region 工作方法
  98. /// <summary>
  99. /// 结束进程操作
  100. /// </summary>
  101. /// <param name="e">参数</param>
  102. private void KillProcessWork(KillProcessStartArgs e)
  103. {
  104. Exception error = null;
  105. bool cancelled = false;
  106. try
  107. {
  108. Process[] processes = Process.GetProcesses();
  109. this.m_Progress.ToComplete = processes.Length;
  110. foreach (Process process in processes)
  111. {
  112. if (this.m_Cancelled)
  113. {
  114. cancelled = true;
  115. return;
  116. }
  117. try
  118. {
  119. if (process.MainModule.FileName.StartsWith(e.Directory, StringComparison.CurrentCultureIgnoreCase))//进程文件在指定目录内
  120. process.Kill();
  121. }
  122. catch
  123. {
  124. }
  125. this.m_Progress.Completed++;
  126. this.PostKillProgressChanged(this.m_Progress, this.m_AsyncOp);
  127. }
  128. }
  129. catch (Exception exp)
  130. {
  131. error = exp;
  132. }
  133. finally
  134. {
  135. this.KillProcessAsyncCallback(error, cancelled, this.m_AsyncOp);
  136. }
  137. }
  138. #endregion
  139. #region 公共方法
  140. /// <summary>
  141. /// 开始异步结束进程
  142. /// </summary>
  143. /// <param name="directory">进程所在目录</param>
  144. /// <param name="userToken">用户数据</param>
  145. public void KillProcessAsync(string directory, object userToken = null)
  146. {
  147. if (directory == null)
  148. throw new ArgumentNullException("directory");
  149. this.InitAsync();
  150. this.ClearState();
  151. AsyncOperation operation = AsyncOperationManager.CreateOperation(userToken);
  152. this.m_AsyncOp = operation;
  153. try
  154. {
  155. this.m_KillProcessStart.BeginInvoke(new KillProcessStartArgs(operation, directory), null, null);
  156. }
  157. catch (Exception error)
  158. {
  159. if (error is ThreadAbortException || error is StackOverflowException || error is OutOfMemoryException)
  160. throw;
  161. this.KillProcessAsyncCallback(error, false, operation);
  162. }
  163. }
  164. #endregion
  165. }
  166. }