Unity Main Thread Dispatcher
Dispatch actions to Unity's main thread from background threads with a simple, thread-safe interface. Provides both synchronous and asynchronous invocation methods, enabling safe UI updates and main-thread operations from worker threads, background tasks, and async code.
com.mainthreaddispatcher.unity.dependencies 
Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/kuraiandras-unity-2.git?path=MainThreadDispatcher.Unity.Dependencies README Markdown
Copy this to your project's README.md
## Installation
Add **Unity Main Thread Dispatcher** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/kuraiandras-unity-2.git?path=MainThreadDispatcher.Unity.Dependencies
```
[](https://www.pkglnk.dev/pkg/kuraiandras-unity-2)README
MainThreadDispatcher
A simple interface to hide platform/framework specific implementations of dispatching to a main thread.
public interface IMainThreadDispatcher
{
/// <summary>
/// Invoke action on main thread. Does not guarantee completion upon return
/// </summary>
/// <param name="action">Action to run</param>
void Invoke(Action action);
/// <summary>
/// Invoke action on main thread
/// </summary>
/// <param name="action">Action to run</param>
/// <returns>Task completes when action is completed</returns>
Task InvokeAsync(Action action);
/// <summary>
/// Invoke action on main thread
/// </summary>
/// <param name="func">Action to run</param>
/// <returns>Task completes when action is completed</returns>
Task<T> InvokeAsync<T>(Func<T> func);
}
Implementations
| Name | Link |
|---|---|
| Unity 3D | |
| WPF | |
| Xamarin.Forms |
MainThreadDispatcher.Unity
Since version 1.1.1 you need to provide the following dlls yourself:
- MainThreadDispatcher
An implementation of the IMainThreadDispatcher interface in Unity 3D
Usage
Add a GameObject to you scene and add the UnityMainThreadDispatcher script to it. Make sure that this GameObject is in your scene.
//Getting an instance with static method
IMainThreadDispatcher dispatcher = UnityMainThreadDispatcherExtensions.Instance;
dispatcher.Invoke(() => Debug.Log("Hello 1"));
await dispatcher.InvokeAsync(() => Debug.Log("Hello 2"))
Button button = await dispatcher.InvokeAsync(() => GameObject.FindObjectOfType<Button>()))
Calling the dispatcher is thread safe
Gotchas
If you call UnityMainThreadDispatcherExtensions.Instance before the the GameObject containing it is instantiated you will get an exception.
If you call UnityMainThreadDispatcherExtensions.Instance for the first time from a thread other than the main unity thread, you will get an exception.
If there are multiple instances in you scene, then when calling UnityMainThreadDispatcherExtensions.Instance it will cache the first it finds. Preferably you only want one instance of it in your application, having multiple does not provide any performance benefit
MainThreadDispatcher.WPF
If you create the WpfMainThreadDispatcher instance on a thread other than the main thread it will not work. It is recommended to create a shared instance. You can achieve this with Dependency Injection or with eagerly creating an Instance with the provided extension class.
MainThreadDispatcher.Xamarin.Forms
This package wraps around Xamarin.Essentials.MainThread, thus you need to set up Xamarin.Essentials.
No comments yet. Be the first!