星火微课系统客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BackgroundWorkerHelper.cs 3.3KB

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