星火批注
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BackgroundWorkerHelper.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.ComponentModel;
  3. using System.Threading;
  4. namespace Common.system
  5. {
  6. /// <summary>
  7. /// 后台线程帮助类
  8. /// </summary>
  9. public class BackgroundWorkerHelper : IDisposable
  10. {
  11. private static BackgroundWorkerHelper backgroundWorkerHelper = null;
  12. public static BackgroundWorkerHelper GetInstance()
  13. {
  14. if (backgroundWorkerHelper == null)
  15. {
  16. backgroundWorkerHelper = new BackgroundWorkerHelper();
  17. }
  18. return backgroundWorkerHelper;
  19. }
  20. private BackgroundWorker backWorker = null;
  21. private Func<object> func = null;
  22. private Action<object> actionComplate = null;
  23. public BackgroundWorkerHelper()
  24. {
  25. backWorker = new BackgroundWorker();
  26. backWorker.DoWork += new DoWorkEventHandler(backWorker_DoWork);
  27. backWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backWorker_RunWorkerCompleted);
  28. }
  29. private void backWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  30. {
  31. currentThread = null;
  32. if (e.Error != null)
  33. {
  34. throw e.Error;
  35. }
  36. if (actionComplate != null)
  37. {
  38. actionComplate(e.Result);
  39. }
  40. }
  41. private void backWorker_DoWork(object sender, DoWorkEventArgs e)
  42. {
  43. currentThread = Thread.CurrentThread;
  44. if (func != null)
  45. {
  46. if (func != null)
  47. {
  48. e.Result = func();
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// 启动异步线程
  54. /// </summary>
  55. /// <param name="func">在异步执行的方法</param>
  56. /// <param name="action">方法完成后</param>
  57. public void RunWorkerAsync(Func<object> func, Action<object> action)
  58. {
  59. if (!backWorker.IsBusy)
  60. {
  61. this.func = func;
  62. actionComplate = action;
  63. backWorker.RunWorkerAsync();
  64. }
  65. }
  66. private Thread currentThread = null;
  67. /// <summary>
  68. /// 强制中断
  69. /// </summary>
  70. public void Abort()
  71. {
  72. if (currentThread != null && backWorker.IsBusy)
  73. {
  74. currentThread.Abort();
  75. currentThread = null;
  76. backWorker.DoWork -= backWorker_DoWork;
  77. backWorker.RunWorkerCompleted -= backWorker_RunWorkerCompleted;
  78. backWorker.Dispose();
  79. backWorker = new BackgroundWorker();
  80. backWorker.DoWork += backWorker_DoWork;
  81. backWorker.RunWorkerCompleted += backWorker_RunWorkerCompleted;
  82. func = null;
  83. actionComplate = null;
  84. }
  85. }
  86. #region IDisposable 成员
  87. /// <summary>
  88. /// 释放资源
  89. /// </summary>
  90. public void Dispose()
  91. {
  92. actionComplate = null;
  93. func = null;
  94. backWorker.Dispose();
  95. }
  96. #endregion IDisposable 成员
  97. }
  98. }