You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. namespace Apewer.Internals
  7. {
  8. internal class KernelHelper
  9. {
  10. private static Thread _freethread = null;
  11. private static int _freesleep = 1000;
  12. private static void FreeListen()
  13. {
  14. while (true)
  15. {
  16. GC.Collect();
  17. Thread.Sleep(_freesleep);
  18. }
  19. }
  20. /// <summary>开始自动释放内存,间隔以毫秒为单位。</summary>
  21. public static void StartFree(int argInterval = 1000)
  22. {
  23. StopFree();
  24. _freesleep = argInterval > 0 ? argInterval : 1000;
  25. _freethread = new Thread(FreeListen);
  26. _freethread.IsBackground = true;
  27. _freethread.Start();
  28. }
  29. /// <summary>停止自动释放内存。</summary>
  30. public static bool StopFree()
  31. {
  32. if (_freethread == null) return true;
  33. try
  34. {
  35. if (_freethread.IsAlive) _freethread.Abort();
  36. _freethread = null;
  37. return true;
  38. }
  39. catch
  40. {
  41. return false;
  42. }
  43. }
  44. }
  45. }