AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AsyncAction.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 #if WINDOWS_UWP
4 using System.Threading.Tasks;
5 #else
6 using System.Threading;
7 #endif
8 
9 namespace UnityGLTF
10 {
14  public class AsyncAction
15  {
16  private bool _workerThreadRunning = false;
17  private Exception _savedException;
18 
19  public IEnumerator RunOnWorkerThread(Action action)
20  {
21  _workerThreadRunning = true;
22 
23 #if WINDOWS_UWP
24  Task.Factory.StartNew(() =>
25 #else
26  ThreadPool.QueueUserWorkItem((_) =>
27 #endif
28  {
29  try
30  {
31  action();
32  }
33  catch (Exception e)
34  {
35  _savedException = e;
36  }
37 
38  _workerThreadRunning = false;
39  });
40 
41  yield return Wait();
42 
43  if (_savedException != null)
44  {
45  throw _savedException;
46  }
47  }
48 
49  private IEnumerator Wait()
50  {
51  while (_workerThreadRunning)
52  {
53  yield return null;
54  }
55  }
56  }
57 }
Creates a thread to run multithreaded operations on
Definition: AsyncAction.cs:14
IEnumerator RunOnWorkerThread(Action action)
Definition: AsyncAction.cs:19