Sync Async - ZjzMisaka/PowerThreadPool GitHub Wiki
PowerThreadPool natively supports both synchronous and asynchronous tasks, enabling seamless mixing of sync and async tasks. It provides consistent lifecycle control and result retrieval mechanisms for both synchronous and asynchronous tasks.
PowerThreadPool allows various ways to submit synchronous tasks using the QueueWorkItem function, including parameterless/parameterized Action, Func, and supports generic parameters and return values.
powerPool.QueueWorkItem(() =>
{
Thread.Sleep(1000);
});
PowerThreadPool natively supports asynchronous tasks of type Task
or Task<TResult>
, which can be submitted via the QueueWorkItemAsync function.
powerPool.QueueWorkItemAsync(async () =>
{
await Task.Delay(1000);
});
PowerThreadPool integrates asynchronous tasks into the unified management system of the thread pool, including scheduling, waiting, grouping, result collection, cancellation, pausing, resuming, and exception handling. This greatly simplifies the complexity of multithreading and asynchronous scenarios in .NET, allowing developers to focus on business logic without worrying about async details.
All features available to synchronous tasks are also fully supported for asynchronous tasks. PowerThreadPool’s unified management of asynchronous tasks does not sacrifice the essential characteristics of asynchrony; instead, it brings their scheduling and lifecycle under unified thread pool management, achieving "unified entry and control." However, under this unified management, the "thread-independence" of async tasks is preserved—they will not be executed synchronously nor forcibly placed into thread pool threads.
PowerThreadPool supports mixing synchronous and asynchronous tasks for unified management. For example:
powerPool.QueueWorkItem(() => {/* synchronous task */});
powerPool.QueueWorkItemAsync(async () => {/* asynchronous task */});
All tasks can be paused, resumed, canceled, grouped, and have their results collected through a consistent interface.