ThreadedRunner.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Threading;
  3. namespace Best.HTTP.Shared.PlatformSupport.Threading
  4. {
  5. [IL2CPP.Il2CppEagerStaticClassConstruction]
  6. public static class ThreadedRunner
  7. {
  8. public static int ShortLivingThreads { get => _shortLivingThreads; }
  9. private static int _shortLivingThreads;
  10. public static int LongLivingThreads { get => _LongLivingThreads; }
  11. private static int _LongLivingThreads;
  12. public static void SetThreadName(string name)
  13. {
  14. try
  15. {
  16. System.Threading.Thread.CurrentThread.Name = name;
  17. }
  18. catch(Exception ex)
  19. {
  20. if (HTTPManager.Logger.IsDiagnostic)
  21. HTTPManager.Logger.Exception(nameof(ThreadedRunner), nameof(SetThreadName), ex);
  22. }
  23. }
  24. public static void RunShortLiving<T>(Action<T> job, T param)
  25. {
  26. ThreadPool.QueueUserWorkItem(new WaitCallback(_ =>
  27. {
  28. using var __ = new IncDecShortLiving(true);
  29. job(param);
  30. }));
  31. }
  32. public static void RunShortLiving<T1, T2>(Action<T1, T2> job, T1 param1, T2 param2)
  33. {
  34. ThreadPool.QueueUserWorkItem(new WaitCallback(_ =>
  35. {
  36. using var __ = new IncDecShortLiving(true);
  37. job(param1, param2);
  38. }));
  39. }
  40. public static void RunShortLiving<T1, T2, T3>(Action<T1, T2, T3> job, T1 param1, T2 param2, T3 param3)
  41. {
  42. ThreadPool.QueueUserWorkItem(new WaitCallback(_ =>
  43. {
  44. using var __ = new IncDecShortLiving(true);
  45. job(param1, param2, param3);
  46. }));
  47. }
  48. public static void RunShortLiving<T1, T2, T3, T4>(Action<T1, T2, T3, T4> job, T1 param1, T2 param2, T3 param3, T4 param4)
  49. {
  50. ThreadPool.QueueUserWorkItem(new WaitCallback(_ =>
  51. {
  52. using var __ = new IncDecShortLiving(true);
  53. job(param1, param2, param3, param4);
  54. }));
  55. }
  56. public static void RunShortLiving(Action job)
  57. {
  58. ThreadPool.QueueUserWorkItem(new WaitCallback((param) =>
  59. {
  60. using var __ = new IncDecShortLiving(true);
  61. job();
  62. }));
  63. }
  64. public static void RunLongLiving(Action job)
  65. {
  66. var thread = new Thread(new ParameterizedThreadStart((param) =>
  67. {
  68. using var __ = new IncDecLongLiving(true);
  69. job();
  70. }));
  71. thread.IsBackground = true;
  72. thread.Start();
  73. }
  74. struct IncDecShortLiving : IDisposable
  75. {
  76. public IncDecShortLiving(bool dummy) => Interlocked.Increment(ref ThreadedRunner._shortLivingThreads);
  77. public void Dispose() => Interlocked.Decrement(ref ThreadedRunner._shortLivingThreads);
  78. }
  79. struct IncDecLongLiving : IDisposable
  80. {
  81. public IncDecLongLiving(bool dummy) => Interlocked.Increment(ref ThreadedRunner._LongLivingThreads);
  82. public void Dispose() => Interlocked.Decrement(ref ThreadedRunner._LongLivingThreads);
  83. }
  84. }
  85. }