ThreadPool.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Threading.ThreadPool
  3. //
  4. // Author:
  5. // Patrik Torstensson
  6. // Dick Porter ([email protected])
  7. // Maurer Dietmar ([email protected])
  8. //
  9. // (C) Ximian, Inc. http://www.ximian.com
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Runtime.CompilerServices;
  14. namespace System.Threading {
  15. public sealed class ThreadPool {
  16. private ThreadPool ()
  17. {
  18. /* nothing to do */
  19. }
  20. public static bool BindHandle (IntPtr osHandle)
  21. {
  22. throw new NotSupportedException("This is MS specific");
  23. }
  24. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  25. public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
  26. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  27. public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
  28. public static bool QueueUserWorkItem (WaitCallback callback)
  29. {
  30. IAsyncResult ar = callback.BeginInvoke (null, null, null);
  31. if (ar == null)
  32. return false;
  33. return true;
  34. }
  35. public static bool QueueUserWorkItem (WaitCallback callback, object state)
  36. {
  37. IAsyncResult ar = callback.BeginInvoke (state, null, null);
  38. if (ar == null)
  39. return false;
  40. return true;
  41. }
  42. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  43. WaitOrTimerCallback callBack,
  44. object state,
  45. int millisecondsTimeOutInterval,
  46. bool executeOnlyOnce)
  47. {
  48. return RegisterWaitForSingleObject (waitObject, callBack, state,
  49. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  50. }
  51. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  52. WaitOrTimerCallback callBack,
  53. object state,
  54. long millisecondsTimeOutInterval,
  55. bool executeOnlyOnce)
  56. {
  57. if (millisecondsTimeOutInterval < -1)
  58. throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
  59. if (millisecondsTimeOutInterval > Int32.MaxValue)
  60. throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
  61. TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
  62. RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
  63. timeout, executeOnlyOnce);
  64. QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
  65. return waiter;
  66. }
  67. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  68. WaitOrTimerCallback callBack,
  69. object state,
  70. TimeSpan timeout,
  71. bool executeOnlyOnce)
  72. {
  73. return RegisterWaitForSingleObject (waitObject, callBack, state,
  74. (long) timeout.TotalMilliseconds, executeOnlyOnce);
  75. }
  76. [CLSCompliant(false)]
  77. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
  78. WaitOrTimerCallback callBack,
  79. object state,
  80. uint millisecondsTimeOutInterval,
  81. bool executeOnlyOnce)
  82. {
  83. return RegisterWaitForSingleObject (waitObject, callBack, state,
  84. (long) millisecondsTimeOutInterval, executeOnlyOnce);
  85. }
  86. public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
  87. {
  88. IAsyncResult ar = callback.BeginInvoke (state, null, null);
  89. if (ar == null)
  90. return false;
  91. return true;
  92. }
  93. }
  94. }