// Execute this work item. public void Execute() { try { // Set the permissions for this stack frame // to the caller's permission set. if (permissions != null) { ClrSecurity.SetPermissions(permissions, 0); } // Run the work item. if (userWorkItem) { if (callback != null) { callback(state); } else if (callback3 != null) { callback3((IAsyncResult)state); } } else { do { if (waitObject.WaitOne(timeout, false)) { if (callback2 != null) { callback2(state, false); } } else if (registered) { if (callback2 != null) { callback2(state, true); } } }while(registered && !once); } } catch (Exception) { // Catch and ignore all exceptions. } }
// Queue a new I/O completion item within the thread pool. // This version is used by the "System" assembly in ECMA_COMPAT // mode when "WaitCallback" is not defined. internal static bool QueueCompletionItem (AsyncCallback callBack, IAsyncResult state) { lock (typeof(ThreadPool)) { if (completionWait == null) { completionWait = new Object(); } } AddCompletionItem (new WorkItem(ClrSecurity.GetPermissionsFrom(1), callBack, state)); return(true); }
// Run a completion thread. private static void Complete() { // Assert unrestricted permissions for this thread. // The permissions will be modified for each work item // to reflect the context that created the work item. ClrSecurity.SetPermissions(null, 0); // Wait for and dispatch completion items. WorkItem item; for (;;) { lock (completionWait) { do { item = CompletionItemToDispatch(); if (item == null) { Monitor.Wait(completionWait); } }while (item == null); usedCompletionThreads++; queuedCompletionItems--; } try { item.Execute(); } finally { item = null; lock (completionWait) usedCompletionThreads--; } } }
// Register a callback to be invoked when a wait handle is available. public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject, WaitOrTimerCallback callBack, Object state, int millisecondsTimeOutInterval, bool executeOnlyOnce) { if (waitObject == null) { throw new ArgumentNullException("waitObject"); } if (millisecondsTimeOutInterval < -1) { throw new ArgumentOutOfRangeException ("millisecondsTimeOutInterval", _("ArgRange_NonNegOrNegOne")); } WorkItem item = new WorkItem(ClrSecurity.GetPermissionsFrom(1), waitObject, callBack, state, millisecondsTimeOutInterval, executeOnlyOnce); AddWorkItem(item); return(new RegisteredWaitHandle(item)); }
// Run a worker thread. private static void Work() { // Assert unrestricted permissions for this thread. // The permissions will be modified for each work item // to reflect the context that created the work item. ClrSecurity.SetPermissions(null, 0); // Wait for and dispatch work items. WorkItem item = null; for (;;) { lock (typeof(ThreadPool)) { do { item = ItemToDispatch(); if (item == null) { Monitor.Wait(typeof(ThreadPool)); } }while(item == null); } try { Interlocked.Increment(ref usedWorkerThreads); item.Execute(); } finally { item = null; Interlocked.Decrement(ref usedWorkerThreads); } } }
// Queue a new work item within the thread pool. public static bool QueueUserWorkItem(WaitCallback callBack, Object state) { AddWorkItem(new WorkItem(ClrSecurity.GetPermissionsFrom(1), callBack, state)); return(true); }