TaskThread.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #if UAP
  2. using System;
  3. using System.Globalization;
  4. using System.Threading.Tasks;
  5. namespace System.Threading
  6. {
  7. internal delegate void ParameterizedThreadStart(object obj);
  8. internal delegate void ThreadStart();
  9. internal class ThreadStateException : Exception
  10. {
  11. public ThreadStateException(string message = "", Exception innerException = null)
  12. : base(message, innerException)
  13. { }
  14. }
  15. internal class Thread
  16. {
  17. private Task _task;
  18. private CancellationTokenSource _tokenSource = new CancellationTokenSource();
  19. public string Name { get; set; }
  20. public bool IsBackground { get; set; }
  21. public bool IsAlive => _task != null && !(_task.IsCanceled || _task.IsCompleted || _task.IsFaulted);
  22. public CultureInfo CurrentCulture => throw new NotImplementedException();
  23. private static SemaphoreSlim _unavailable = new SemaphoreSlim(0, 1);
  24. private enum StartType
  25. {
  26. Standard,
  27. Parameterized
  28. };
  29. StartType _startType;
  30. ParameterizedThreadStart _parameterizedStart;
  31. ThreadStart _start;
  32. public Thread(ParameterizedThreadStart threadStart, int maxStackSize = 0)
  33. {
  34. _startType = StartType.Parameterized;
  35. _parameterizedStart = threadStart;
  36. }
  37. public Thread(ThreadStart threadStart, int maxStackSize = 0)
  38. {
  39. _startType = StartType.Standard;
  40. _start = threadStart;
  41. }
  42. public void Start()
  43. {
  44. if (_startType == StartType.Parameterized)
  45. {
  46. throw new InvalidOperationException("Must supply argument for ParameterizedThreadStart!");
  47. }
  48. if (_task != null)
  49. {
  50. throw new ThreadStateException("Thread already started!");
  51. }
  52. _task = new Task(() => _start(), _tokenSource.Token, TaskCreationOptions.LongRunning);
  53. _task.Start();
  54. }
  55. public void Start(object obj)
  56. {
  57. if (_startType == StartType.Standard)
  58. {
  59. throw new InvalidOperationException("Must use parameterless Start() method instead!");
  60. }
  61. if (_task != null)
  62. {
  63. throw new ThreadStateException("Thread already started!");
  64. }
  65. _task = new Task(() => _parameterizedStart(obj), _tokenSource.Token, TaskCreationOptions.LongRunning);
  66. _task.Start();
  67. }
  68. public void Join()
  69. {
  70. _task.Wait();
  71. }
  72. public bool Join(Int32 milliseconds)
  73. {
  74. return _task.Wait(milliseconds);
  75. }
  76. public bool Join(TimeSpan timeout)
  77. {
  78. return _task.Wait(timeout);
  79. }
  80. public static void Sleep(int milliseconds)
  81. {
  82. _unavailable.Wait(milliseconds);
  83. }
  84. public static void Sleep(TimeSpan duration)
  85. {
  86. _unavailable.Wait(duration);
  87. }
  88. }
  89. }
  90. #endif