// 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. } }
// 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--; } } }
// 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); } } }