2024年1月27日发(作者:)

///

/// Informs the ThreadPool that there's work to be executed for this scheduler.

///

private void NotifyThreadPoolOfPendingWork() { QueueUserWorkItem(_ => { // Note that the current thread is now processing work items.

// This is necessary to enable inlining of tasks into this thread.

_currentThreadIsProcessingItems = true; try { // Process all available items in the queue.

while (true) { Task item; lock (_tasks) { // When there are no more items to be processed,

// note that we're done processing, and get out.

if (_ == 0) { --_delegatesQueuedOrRunning; break; }

// Get the next item from the queue

item = _; _First(); }

// Execute the task we pulled out of the queue

cuteTask(item); } } // We're done processing items on the current thread

finally { _currentThreadIsProcessingItems = false; } }, null); } ///

Attempts to execute the specified task on the current thread.

/// The task to be executed.

///

/// Whether the task could be executed on the current thread.

protected sealed override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) { // If this thread isn't already processing a task, we don't support inlining

if (!_currentThreadIsProcessingItems) return false; // If the task was previously queued, remove it from the queue

if (taskWasPreviouslyQueued) TryDequeue(task); // Try to run the task.

return cuteTask(task); } ///

Attempts to remove a previously scheduled task from the scheduler.

/// The task to be removed.

/// Whether the task could be found and removed.

protected sealed override bool TryDequeue(Task task) { lock (_tasks) return _(task); }