Class ThreadUtils
Provides utility methods for creating and managing threads (or tasks) in TriLib, offering asynchronous execution, error handling, cancellation, and progress reporting.
Inherited Members
Namespace: TriLibCore.Utils
Assembly: TriLibCore.dll
Syntax
public static class ThreadUtils
Methods
RequestNewThreadFor<T>(T, Action<T>, Action<T>, Action<IContextualizedError>, int, string, bool, Action<T>)
Starts a new task (thread) to execute onStart using the provided T context.
If the AssetLoaderContext Async property is false,
this method will execute onStart synchronously on the main thread.
Declaration
public static Task RequestNewThreadFor<T>(T context, Action<T> onStart, Action<T> onComplete = null, Action<IContextualizedError> onError = null, int timeout = 0, string name = null, bool startImmediately = true, Action<T> onCompleteSameThread = null) where T : class, IAssetLoaderContext
Parameters
| Type | Name | Description |
|---|---|---|
| T | context | The context which will be passed to the |
| Action<T> | onStart | The action to execute on a background thread (if asynchronous) or on the main thread (if synchronous). |
| Action<T> | onComplete | An optional action to call on the main thread once |
| Action<IContextualizedError> | onError | An optional action to call on the main thread if an exception occurs. If not provided, the error is rethrown on the main thread as a ContextualizedError<T>. |
| int | timeout | The thread timeout in seconds. If nonzero, the operation is canceled after this duration. |
| string | name | An optional name for the worker thread. |
| bool | startImmediately | true to start the task immediately; false to create it without starting. |
| Action<T> | onCompleteSameThread | An optional action to execute on the worker thread immediately after |
Returns
| Type | Description |
|---|---|
| Task | A Task representing the asynchronous operation if AssetLoaderContext Async is |
Type Parameters
| Name | Description |
|---|---|
| T | A class type that implements IAssetLoaderContext, representing the thread's shared context. |
RunThreadSimple(Action, int, string, bool, int)
Starts a lightweight task on a new thread, optionally waiting before execution and without specifying a context, completion callback, or error callback.
Declaration
public static Task RunThreadSimple(Action onStart, int timeout = 0, string name = null, bool startImmediately = true, int waitMilliseconds = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | onStart | The action to execute on the new thread. |
| int | timeout | The thread timeout in seconds, not currently applied. Provided for API consistency. |
| string | name | An optional name for the thread. |
| bool | startImmediately | true to start the task immediately; false to create it without starting. |
| int | waitMilliseconds | The delay in milliseconds before |
Returns
| Type | Description |
|---|---|
| Task | A Task representing the new thread. |
RunThread<T>(T, ref CancellationToken, Action<T>, Action<T>, Action<IContextualizedError>, int, string, bool)
Starts a new task on a background thread using the specified context T
and a cancellationToken. Once execution completes (or fails), the optional onComplete
or onError will be invoked on the main thread.
Declaration
public static Task RunThread<T>(T context, ref CancellationToken cancellationToken, Action<T> onStart, Action<T> onComplete = null, Action<IContextualizedError> onError = null, int timeout = 0, string name = null, bool startImmediately = true) where T : IAssetLoaderContext
Parameters
| Type | Name | Description |
|---|---|---|
| T | context | The asset loader context on which this thread operation should act. |
| CancellationToken | cancellationToken | A CancellationToken that can be used to halt the operation if canceled.
If |
| Action<T> | onStart | The action to execute on the new thread. |
| Action<T> | onComplete | An optional action invoked on the main thread once |
| Action<IContextualizedError> | onError | An optional action invoked on the main thread if an exception occurs. If not specified, the error is rethrown on the main thread as a ContextualizedError<T>. |
| int | timeout | The thread timeout in seconds. If nonzero, the task is canceled after this duration. |
| string | name | An optional name for the thread. |
| bool | startImmediately | true to start the task immediately; false to create it without starting. |
Returns
| Type | Description |
|---|---|
| Task | The Task that was created, possibly not started if |
Type Parameters
| Name | Description |
|---|---|
| T | The context type that implements IAssetLoaderContext;
it is passed to |