Exemplo n.º 1
0
        public MondValue Start(MondState state, MondValue value)
        {
            if (value.Type == MondValueType.Function)
            {
                value = state.Call(value);
            }

            var getEnumerator = value["getEnumerator"];

            if (getEnumerator.Type != MondValueType.Function)
            {
                throw new MondRuntimeException("Task objects must define getEnumerator");
            }

            var enumerator = state.Call(getEnumerator);

            var task = _factory.StartNew(async() =>
            {
                try
                {
                    await AsyncUtil.RunMondTask(state, enumerator);
                }
                catch (Exception e)
                {
                    lock (_exceptions)
                        _exceptions.Enqueue(e);
                }
                finally
                {
                    Interlocked.Decrement(ref _activeTasks);
                }
            });

            Interlocked.Increment(ref _activeTasks);

            // return a task that completes when the started task completes
            Func <Task> waitTask = async() =>
            {
                await await task;
            };

            return(AsyncUtil.ToObject(waitTask()));
        }