using System; using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; using System.Diagnostics; namespace Unity.Services.Relay.Scheduler { /// /// Provides the means to schedule tasks on a background thread or the /// main thread. Also allows for creation of co-routines from classes that do /// not inherit from MonoBehaviour. /// /// This is thread safe, through it must be constructed on the main thread. /// /// public abstract class TaskScheduler : MonoBehaviour { /// /// Schedules a new task on a background thread. /// /// /// The task that should be executed on a background thread. public abstract void ScheduleBackgroundTask(Action task); /// /// Determines whether the current thread is the main thread. /// /// /// Whether or not this thread is the main thread. public abstract bool IsMainThread(); /// /// Schedules a new task on the main thread. The task will be executed during the /// next update. /// /// /// The task that should be executed on the main thread. public abstract void ScheduleMainThreadTask(Action task); /// /// Executes immediately if on main thread else queue on main thread for next update. /// /// /// The task that should be executed on the main thread. public void ScheduleOrExecuteOnMain(Action action) { if (IsMainThread()) { action?.Invoke(); } else { ScheduleMainThreadTask(action); } } } }