Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions UnityMainThreadDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ public void Enqueue(Action action)
{
Enqueue(ActionWrapper(action));
}

/// <summary>
/// Locks the queue and adds the Action to the queue, returning a Task which is completed when the action completes
/// </summary>
/// <param name="action">function that will be executed from the main thread.</param>
/// <returns>A Task that can be awaited until the action completes</returns>
public Task EnqueueAsync(Action action)
{
var tcs = new TaskCompletionSource<bool>();

void WrappedAction() {
try
{
action();
tcs.TrySetResult(true);
} catch (Exception ex)
{
tcs.TrySetException(ex);
}
}

Enqueue(ActionWrapper(WrappedAction));
return tcs.Task;
}


IEnumerator ActionWrapper(Action a)
{
a();
Expand Down