Class ThreadHelper
- Namespace
- WindowSill.API
- Assembly
- WindowSill.API.dll
Provides helper methods for managing thread access, particularly for UI thread operations.
public static class ThreadHelper
- Inheritance
-
ThreadHelper
- Inherited Members
Methods
RunOnUIThreadAsync(DispatcherQueuePriority, Action)
Executes the specified action on the UI thread asynchronously.
public static Task RunOnUIThreadAsync(DispatcherQueuePriority priority, Action action)
Parameters
priorityDispatcherQueuePriorityThe priority at which the action should be executed on the UI thread.
actionActionThe action to execute.
Returns
- Task
A Task that represents the asynchronous operation. The task completes when the action has been executed.
Remarks
If the current thread already has access to the UI thread and the specified priority is Normal, the action is executed immediately on the calling thread. Otherwise, the action is enqueued to the UI thread with the specified priority.
RunOnUIThreadAsync(DispatcherQueuePriority, Func<Task>)
Executes the specified asynchronous action on the UI thread with the given priority.
public static Task RunOnUIThreadAsync(DispatcherQueuePriority priority, Func<Task> action)
Parameters
priorityDispatcherQueuePriorityThe priority at which the action should be executed on the UI thread.
actionFunc<Task>The asynchronous action to execute.
Returns
- Task
A task that represents the completion of the action on the UI thread.
Remarks
If the current thread already has access to the UI thread and the priority is Normal, the action is executed directly. Otherwise, the action is enqueued to the UI thread with the specified priority.
RunOnUIThreadAsync(Action)
Executes the specified action asynchronously on the UI thread.
public static Task RunOnUIThreadAsync(Action action)
Parameters
Returns
RunOnUIThreadAsync(Func<Task>)
Executes the specified asynchronous action on the UI thread.
public static Task RunOnUIThreadAsync(Func<Task> action)
Parameters
Returns
- Task
A task that represents the completion of the action.
RunOnUIThreadAsync<T>(DispatcherQueuePriority, Func<Task<T>>)
Executes the specified asynchronous action on the UI thread with the given priority and returns its result.
public static Task<T> RunOnUIThreadAsync<T>(DispatcherQueuePriority priority, Func<Task<T>> action)
Parameters
priorityDispatcherQueuePriorityThe priority at which the action should be executed on the UI thread.
actionFunc<Task<T>>The asynchronous action to execute. This action must not be null.
Returns
- Task<T>
A task that represents the asynchronous operation. The task's result is the value returned by the action.
Type Parameters
TThe type of the result returned by the asynchronous action.
Remarks
If the current thread already has access to the UI thread and the priority is Normal, the action is executed directly on the current thread. Otherwise, the action is enqueued to the UI thread with the specified priority.
RunOnUIThreadAsync<T>(DispatcherQueuePriority, Func<T>)
Executes the specified function on the UI thread asynchronously and returns its result.
public static Task<T> RunOnUIThreadAsync<T>(DispatcherQueuePriority priority, Func<T> func)
Parameters
priorityDispatcherQueuePriorityThe priority with which the function should be executed on the UI thread.
funcFunc<T>The function to execute on the UI thread.
Returns
- Task<T>
A task that represents the asynchronous operation. The task's result is the value returned by the function. If
funcis null, the task's result will be the default value of typeT.
Type Parameters
TThe type of the result returned by the function.
Remarks
If the current thread already has access to the UI thread and the specified priority is Normal, the function is executed synchronously on the current thread. Otherwise, the function is enqueued to the UI thread with the specified priority.
RunOnUIThreadAsync<T>(Func<Task<T>>)
Executes the specified asynchronous action on the UI thread and returns its result.
public static Task<T> RunOnUIThreadAsync<T>(Func<Task<T>> action)
Parameters
Returns
- Task<T>
A task that represents the asynchronous operation. The task's result contains the value returned by the action.
Type Parameters
TThe type of the result returned by the asynchronous action.
RunOnUIThreadAsync<T>(Func<T>)
Executes the specified function on the UI thread and returns its result.
public static Task<T> RunOnUIThreadAsync<T>(Func<T> func)
Parameters
funcFunc<T>The function to execute on the UI thread.
Returns
- Task<T>
A task that represents the asynchronous operation. The task's result is the value returned by the function.
Type Parameters
TThe type of the result returned by the function.
ThrowIfNotOnUIThread()
Ensures that the current code is executing on the UI thread.
public static void ThrowIfNotOnUIThread()
Remarks
This method verifies that the current thread has access to the UI dispatcher. If the method is called from a non-UI thread, an exception is thrown. Use this method to enforce thread affinity for operations that must run on the UI thread.
Exceptions
- Exception
Thrown if the current thread does not have access to the UI dispatcher.
ThrowIfOnUIThread()
Throws an exception if the current call is being executed on the UI thread.
public static void ThrowIfOnUIThread()
Remarks
This method is typically used to enforce that certain operations are not performed on the UI thread, which can help prevent UI thread blocking or deadlocks. Ensure that this method is called only in contexts where UI thread access is not allowed.
Exceptions
- Exception
Thrown if the current call stack is running on the UI thread.