SpicyPixel.Threading.Tasks.FiberTaskScheduler.ExecuteYieldableTask C# (CSharp) Method

ExecuteYieldableTask() private method

Execute the specified coroutine associated with a yieldable task.
Any exceptions that occur while executing the fiber will be associated with the specified task and rethrown by the framework.
private ExecuteYieldableTask ( YieldableTask task ) : IEnumerator
task YieldableTask /// The task associated with the executing fiber. ///
return IEnumerator
        private IEnumerator ExecuteYieldableTask(YieldableTask task)
        {
            object fiberResult = null;

            // TODO: Cleanup
            //
            // This uses internal setters to fake starting the fiber
            // and then uses internal Execute(). It should be possible to
            // use public APIs instead to:
            //   1. Set a fiber property to associate the task
            //   2. Set an exception handler on the FiberScheduler
            //   3. Retrieve the task in the handler from the fiber and set
            //      the exeption
            //   4. Return from the handler without rethrowing
            //
            // This method could be removed then and the internal setters
            // removed as well.
            task.Fiber.Scheduler = scheduler;
            task.Fiber.Status = FiberStatus.Running;

            while(true)
            {
                try
                {
                    // Throw here because the scheduler is disposed and will not
                    // run anything else. No further access is valid.
                    CancellationToken.ThrowIfCancellationRequested();

                    fiberResult = task.Fiber.Execute();

                    if(fiberResult is StopInstruction) {
                        // Check for exceptions
                        if (task.Fiber.IsFaulted) {
                            task.FiberException = task.Fiber.Exception;
                        }

                        yield break;
                    }
                }
                catch(System.Threading.OperationCanceledException ex)
                {
                    // Fiber execution does not throw so the only exception
                    // expected is the cancellation above.
                    task.FiberException = ex;
                    yield break;
                }

                yield return fiberResult;
            }
        }