12 [__DynamicallyInvokable]
13 [HostProtection(
SecurityAction.LinkDemand, Synchronization =
true, ExternalThreading =
true)]
21 [SecuritySafeCritical]
23 public static bool SetMaxThreads(
int workerThreads,
int completionPortThreads)
25 return SetMaxThreadsNative(workerThreads, completionPortThreads);
31 [SecuritySafeCritical]
32 public static void GetMaxThreads(out
int workerThreads, out
int completionPortThreads)
34 GetMaxThreadsNative(out workerThreads, out completionPortThreads);
43 [SecuritySafeCritical]
45 public static bool SetMinThreads(
int workerThreads,
int completionPortThreads)
47 return SetMinThreadsNative(workerThreads, completionPortThreads);
55 [SecuritySafeCritical]
56 public static void GetMinThreads(out
int workerThreads, out
int completionPortThreads)
58 GetMinThreadsNative(out workerThreads, out completionPortThreads);
64 [SecuritySafeCritical]
67 GetAvailableThreadsNative(out workerThreads, out completionPortThreads);
80 [SecuritySafeCritical]
84 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
85 return RegisterWaitForSingleObject(waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce, ref stackMark, compressStack:
true);
102 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
103 return RegisterWaitForSingleObject(waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce, ref stackMark, compressStack:
false);
113 RegisteredWaitHandle registeredWaitHandle =
new RegisteredWaitHandle();
114 if (callBack !=
null)
116 _ThreadPoolWaitOrTimerCallback threadPoolWaitOrTimerCallback =
new _ThreadPoolWaitOrTimerCallback(callBack, state, compressStack, ref stackMark);
117 state = threadPoolWaitOrTimerCallback;
118 registeredWaitHandle.SetWaitObject(waitObject);
119 IntPtr handle = RegisterWaitForSingleObjectNative(waitObject, state, millisecondsTimeOutInterval, executeOnlyOnce, registeredWaitHandle, ref stackMark, compressStack);
120 registeredWaitHandle.SetHandle(handle);
121 return registeredWaitHandle;
123 throw new ArgumentNullException(
"WaitOrTimerCallback");
136 [SecuritySafeCritical]
139 if (millisecondsTimeOutInterval < -1)
143 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
144 return RegisterWaitForSingleObject(waitObject, callBack, state, (uint)millisecondsTimeOutInterval, executeOnlyOnce, ref stackMark, compressStack:
true);
161 if (millisecondsTimeOutInterval < -1)
165 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
166 return RegisterWaitForSingleObject(waitObject, callBack, state, (uint)millisecondsTimeOutInterval, executeOnlyOnce, ref stackMark, compressStack:
false);
179 [SecuritySafeCritical]
182 if (millisecondsTimeOutInterval < -1)
186 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
187 return RegisterWaitForSingleObject(waitObject, callBack, state, (uint)millisecondsTimeOutInterval, executeOnlyOnce, ref stackMark, compressStack:
true);
204 if (millisecondsTimeOutInterval < -1)
208 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
209 return RegisterWaitForSingleObject(waitObject, callBack, state, (uint)millisecondsTimeOutInterval, executeOnlyOnce, ref stackMark, compressStack:
false);
223 [SecuritySafeCritical]
231 if (num >
int.MaxValue)
235 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
259 if (num >
int.MaxValue)
263 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
276 [SecuritySafeCritical]
277 [__DynamicallyInvokable]
280 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
281 return QueueUserWorkItemHelper(callBack, state, ref stackMark, compressStack:
true);
292 [SecuritySafeCritical]
293 [__DynamicallyInvokable]
296 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
297 return QueueUserWorkItemHelper(callBack,
null, ref stackMark, compressStack:
true);
314 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
315 return QueueUserWorkItemHelper(callBack, state, ref stackMark, compressStack:
false);
319 private static bool QueueUserWorkItemHelper(
WaitCallback callBack,
object state, ref StackCrawlMark stackMark,
bool compressStack)
322 if (callBack !=
null)
324 EnsureVMInitialized();
331 QueueUserWorkItemCallback callback =
new QueueUserWorkItemCallback(callBack, state, compressStack, ref stackMark);
332 ThreadPoolGlobals.workQueue.Enqueue(callback, forceGlobal:
true);
336 throw new ArgumentNullException(
"WaitCallback");
340 internal static void UnsafeQueueCustomWorkItem(IThreadPoolWorkItem workItem,
bool forceGlobal)
342 EnsureVMInitialized();
348 ThreadPoolGlobals.workQueue.Enqueue(workItem, forceGlobal);
353 internal static bool TryPopCustomWorkItem(IThreadPoolWorkItem workItem)
355 if (!ThreadPoolGlobals.vmTpInitialized)
359 return ThreadPoolGlobals.workQueue.LocalFindAndPop(workItem);
365 return EnumerateQueuedWorkItems(ThreadPoolWorkQueue.allThreadQueues.Current, ThreadPoolGlobals.workQueue.queueTail);
368 internal static IEnumerable<IThreadPoolWorkItem> EnumerateQueuedWorkItems(ThreadPoolWorkQueue.WorkStealingQueue[] wsQueues, ThreadPoolWorkQueue.QueueSegment globalQueueTail)
370 if (wsQueues !=
null)
372 foreach (ThreadPoolWorkQueue.WorkStealingQueue workStealingQueue in wsQueues)
374 if (workStealingQueue ==
null || workStealingQueue.m_array ==
null)
378 IThreadPoolWorkItem[] items2 = workStealingQueue.m_array;
379 foreach (IThreadPoolWorkItem threadPoolWorkItem
in items2)
381 if (threadPoolWorkItem !=
null)
383 yield
return threadPoolWorkItem;
388 if (globalQueueTail ==
null)
392 for (ThreadPoolWorkQueue.QueueSegment segment = globalQueueTail; segment !=
null; segment = segment.Next)
394 IThreadPoolWorkItem[] items = segment.nodes;
395 foreach (IThreadPoolWorkItem threadPoolWorkItem2
in items)
397 if (threadPoolWorkItem2 !=
null)
399 yield
return threadPoolWorkItem2;
408 return EnumerateQueuedWorkItems(
new ThreadPoolWorkQueue.WorkStealingQueue[1]
410 ThreadPoolWorkQueueThreadLocals.threadLocals.workStealingQueue
417 return EnumerateQueuedWorkItems(
null, ThreadPoolGlobals.workQueue.queueTail);
423 foreach (IThreadPoolWorkItem workitem
in workitems)
427 object[] array =
new object[num];
429 foreach (IThreadPoolWorkItem workitem2
in workitems)
431 if (num < array.Length)
433 array[num] = workitem2;
441 internal static object[] GetQueuedWorkItemsForDebugger()
443 return ToObjectArray(GetQueuedWorkItems());
447 internal static object[] GetGloballyQueuedWorkItemsForDebugger()
449 return ToObjectArray(GetGloballyQueuedWorkItems());
453 internal static object[] GetLocallyQueuedWorkItemsForDebugger()
455 return ToObjectArray(GetLocallyQueuedWorkItems());
460 [SuppressUnmanagedCodeSecurity]
461 internal static extern bool RequestWorkerThread();
465 private unsafe
static extern bool PostQueuedCompletionStatus(NativeOverlapped* overlapped);
472 [CLSCompliant(
false)]
475 return PostQueuedCompletionStatus(overlapped);
479 private static void EnsureVMInitialized()
481 if (!ThreadPoolGlobals.vmTpInitialized)
483 InitializeVMTp(ref ThreadPoolGlobals.enableWorkerTracking);
484 ThreadPoolGlobals.vmTpInitialized =
true;
490 private static extern bool SetMinThreadsNative(
int workerThreads,
int completionPortThreads);
494 private static extern bool SetMaxThreadsNative(
int workerThreads,
int completionPortThreads);
498 private static extern void GetMinThreadsNative(out
int workerThreads, out
int completionPortThreads);
502 private static extern void GetMaxThreadsNative(out
int workerThreads, out
int completionPortThreads);
506 private static extern void GetAvailableThreadsNative(out
int workerThreads, out
int completionPortThreads);
510 internal static extern bool NotifyWorkItemComplete();
514 internal static extern void ReportThreadStatus(
bool isWorking);
516 [SecuritySafeCritical]
517 internal static void NotifyWorkItemProgress()
519 if (!ThreadPoolGlobals.vmTpInitialized)
521 InitializeVMTp(ref ThreadPoolGlobals.enableWorkerTracking);
523 NotifyWorkItemProgressNative();
528 internal static extern void NotifyWorkItemProgressNative();
532 internal static extern bool IsThreadPoolHosted();
536 [SuppressUnmanagedCodeSecurity]
537 private static extern void InitializeVMTp(ref
bool enableWorkerTracking);
541 private static extern IntPtr RegisterWaitForSingleObjectNative(WaitHandle waitHandle,
object state, uint timeOutInterval,
bool executeOnlyOnce, RegisteredWaitHandle registeredWaitHandle, ref StackCrawlMark stackMark,
bool compressStack);
548 [SecuritySafeCritical]
549 [Obsolete(
"ThreadPool.BindHandle(IntPtr) has been deprecated. Please use ThreadPool.BindHandle(SafeHandle) instead.",
false)]
553 return BindIOCompletionCallbackNative(osHandle);
562 [SecuritySafeCritical]
566 if (osHandle ==
null)
571 bool success =
false;
589 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
590 private static extern bool BindIOCompletionCallbackNative(
IntPtr fileHandle);
static unsafe bool UnsafeQueueNativeOverlapped(NativeOverlapped *overlapped)
Queues an overlapped I/O operation for execution.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
Ability to use certain advanced operations on threads.
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
Represents a handle that has been registered when calling M:System.Threading.ThreadPool....
static bool QueueUserWorkItem(WaitCallback callBack, object state)
Queues a method for execution, and specifies an object containing data to be used by the method....
The exception that is thrown when the value of an argument is outside the allowable range of values a...
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object state)
Queues the specified delegate to the thread pool, but does not propagate the calling stack to the wor...
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
static void GetMinThreads(out int workerThreads, out int completionPortThreads)
Retrieves the minimum number of threads the thread pool creates on demand, as new requests are made,...
Represents a wrapper class for operating system handles. This class must be inherited.
Cer
Specifies a method's behavior when called within a constrained execution region.
static bool SetMinThreads(int workerThreads, int completionPortThreads)
Sets the minimum number of threads the thread pool creates on demand, as new requests are made,...
void DangerousRelease()
Manually decrements the reference counter on a T:System.Runtime.InteropServices.SafeHandle instance.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
static void GetAvailableThreads(out int workerThreads, out int completionPortThreads)
Retrieves the difference between the maximum number of thread pool threads returned by the M:System....
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a 32-bit unsigned intege...
void DangerousAddRef(ref bool success)
Manually increments the reference counter on T:System.Runtime.InteropServices.SafeHandle instances.
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, TimeSpan timeout, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a T:System....
A platform-specific type that is used to represent a pointer or a handle.
delegate void WaitOrTimerCallback(object state, bool timedOut)
Represents a method to be called when a T:System.Threading.WaitHandle is signaled or times out.
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, using a 32-bit signed integer for t...
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a 32-bit unsigned intege...
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a 64-bit signed integer ...
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
static bool IsTransparentProxy(object proxy)
Returns a Boolean value that indicates whether the given object is a transparent proxy or a real obje...
IntPtr DangerousGetHandle()
Returns the value of the F:System.Runtime.InteropServices.SafeHandle.handle field.
static bool BindHandle(IntPtr osHandle)
Binds an operating system handle to the T:System.Threading.ThreadPool.
Represents a time interval.To browse the .NET Framework source code for this type,...
The exception that is thrown when a method call is invalid for the object's current state.
static bool QueueUserWorkItem(WaitCallback callBack)
Queues a method for execution. The method executes when a thread pool thread becomes available.
Consistency
Specifies a reliability contract.
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a 32-bit signed integer ...
static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, TimeSpan timeout, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a T:System....
delegate void WaitCallback(object state)
Represents a callback method to be executed by a thread pool thread.
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides several methods for using and publishing remoted objects and proxies. This class cannot be i...
static bool BindHandle(SafeHandle osHandle)
Binds an operating system handle to the T:System.Threading.ThreadPool.
static void GetMaxThreads(out int workerThreads, out int completionPortThreads)
Retrieves the number of requests to the thread pool that can be active concurrently....
Provides an explicit layout that is visible from unmanaged code and that will have the same layout as...
static bool SetMaxThreads(int workerThreads, int completionPortThreads)
Sets the number of requests to the thread pool that can be active concurrently. All requests above th...
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Provides a set of static methods and properties that provide support for compilers....
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a 64-bit signed integer ...