Class ThreadUtils
Provides helper methods for launching Threads.
Namespace: NanoByte.Common.Threading
Assembly: NanoByte.Common.dll
Syntax
public static class ThreadUtils : Object
Methods
RunSta(Action)
Executes a delegate in a new STA thread. Blocks the caller until the execution completes.
Declaration
public static void RunSta(Action execute)
Parameters
Type | Name | Description |
---|---|---|
Action | execute | The delegate to execute. |
Remarks
This is useful for code that needs to be executed in a Single-Threaded Apartment (e.g. WinForms code) when the calling thread is not set up to handle COM.
RunSta<T>(Func<T>)
Executes a delegate in a new STA thread. Blocks the caller until the execution completes.
Declaration
public static T RunSta<T>(Func<T> execute)
Parameters
Type | Name | Description |
---|---|---|
Func<T> | execute | The delegate to execute. |
Returns
Type | Description |
---|---|
T | The return value of |
Type Parameters
Name | Description |
---|---|
T | The type of the return value of |
Remarks
This is useful for code that needs to be executed in a Single-Threaded Apartment (e.g. WinForms code) when the calling thread is not set up to handle COM.
RunTask(Func<Task>)
Runs an asynchronous task and blocks until it completes. Avoids deadlocks by ignoring the SynchronizationContext. Rethrows exceptions thrown inside task without AggregateException wrapper.
Declaration
public static void RunTask(Func<Task> action)
Parameters
Type | Name | Description |
---|---|---|
Func<Task> | action | Callback for starting the task. |
Exceptions
Type | Condition |
---|---|
TaskCanceledException | The task was cancelled. |
RunTask<T>(Func<Task<T>>)
Runs an asynchronous task and blocks until it completes. Avoids deadlocks by ignoring the SynchronizationContext. Rethrows exceptions thrown inside task without AggregateException wrapper.
Declaration
public static T RunTask<T>(Func<Task<T>> action)
Parameters
Type | Name | Description |
---|---|---|
Func<System.Threading.Tasks.Task<T>> | action | Callback for starting the task. |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
Exceptions
Type | Condition |
---|---|
TaskCanceledException | The task was cancelled. |
StartAsync(ThreadStart, String)
Starts executing a delegate in a new thread suitable for WinForms.
Declaration
public static Thread StartAsync(ThreadStart execute, string name = null)
Parameters
Type | Name | Description |
---|---|---|
System.Threading.ThreadStart | execute | The delegate to execute. |
String | name | A short name for the new thread; can be |
Returns
Type | Description |
---|---|
Thread | The newly launched thread. |
StartBackground(ThreadStart, String)
Starts executing a delegate in a new background thread (automatically terminated when application exits).
Declaration
public static Thread StartBackground(ThreadStart execute, string name = null)
Parameters
Type | Name | Description |
---|---|---|
System.Threading.ThreadStart | execute | The delegate to execute. |
String | name | A short name for the new thread; can be |
Returns
Type | Description |
---|---|
Thread | The newly launched thread. |