1
0

ThreadPoolScheduler_UnityWinRT.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #if UNITY_METRO
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. #if NETFX_CORE
  9. using System.Threading.Tasks;
  10. #endif
  11. namespace UniRx
  12. {
  13. public static partial class Scheduler
  14. {
  15. public static readonly IScheduler ThreadPool = new ThreadPoolScheduler();
  16. class ThreadPoolScheduler : IScheduler
  17. {
  18. public DateTimeOffset Now
  19. {
  20. get { return Scheduler.Now; }
  21. }
  22. public IDisposable Schedule(Action action)
  23. {
  24. var d = new BooleanDisposable();
  25. #if NETFX_CORE
  26. Task.Run(()=>
  27. {
  28. if (!d.IsDisposed)
  29. {
  30. action();
  31. }
  32. });
  33. #else
  34. Action act = () =>
  35. {
  36. if (!d.IsDisposed)
  37. {
  38. action();
  39. }
  40. };
  41. act.BeginInvoke(ar => act.EndInvoke(ar), null);
  42. #endif
  43. return d;
  44. }
  45. public IDisposable Schedule(TimeSpan dueTime, Action action)
  46. {
  47. var wait = Scheduler.Normalize(dueTime);
  48. var d = new BooleanDisposable();
  49. #if NETFX_CORE
  50. Task.Run(()=>
  51. {
  52. if (!d.IsDisposed)
  53. {
  54. if (wait.Ticks > 0)
  55. {
  56. Thread.Sleep(wait);
  57. }
  58. action();
  59. }
  60. });
  61. #else
  62. Action act = () =>
  63. {
  64. if (!d.IsDisposed)
  65. {
  66. if (wait.Ticks > 0)
  67. {
  68. Thread.Sleep(wait);
  69. }
  70. action();
  71. }
  72. };
  73. act.BeginInvoke(ar => act.EndInvoke(ar), null);
  74. #endif
  75. return d;
  76. }
  77. }
  78. }
  79. }
  80. #endif