holopy3/Assets/PlattarExporter/UnityGLTF/Scripts/Async/AsyncAction.cs
Lena Biresch 490ef558ef CleanUp
2021-01-28 13:07:52 +01:00

51 lines
792 B
C#

using System;
using System.Collections;
using System.Threading;
namespace UnityGLTF
{
/// <summary>
/// Creates a thread to run multithreaded operations on
/// </summary>
public class AsyncAction
{
private bool _workerThreadRunning = false;
private Exception _savedException;
#if !WINDOWS_UWP
public IEnumerator RunOnWorkerThread(Action action)
{
_workerThreadRunning = true;
ThreadPool.QueueUserWorkItem((_) =>
{
try
{
action();
}
catch (Exception e)
{
_savedException = e;
}
_workerThreadRunning = false;
});
yield return Wait();
if (_savedException != null)
{
throw _savedException;
}
}
private IEnumerator Wait()
{
while (_workerThreadRunning)
{
yield return null;
}
}
#endif
}
}