public void TestYield()
        {
            ThreadCpuGovernor governor = new ThreadCpuGovernor(50);

            Assert.DoesNotThrow(() => governor.Yield());
            Assert.DoesNotThrow(() => governor.Yield());
            Assert.DoesNotThrow(() => governor.Yield());
        }
        /// <summary>
        /// Writes the tokens to the stream.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void OnGenerate(WriterContext context)
        {
            ThreadCpuGovernor threadGovernor = new ThreadCpuGovernor(50);

            foreach (Token token in Tokens)
            {
                // Allocate new unique IDs for recurring images
                var docProperties = token.SourceNode as  DocProperties;
                if (token.IsOpen && docProperties != null)
                {
                    docProperties.Id = context.AllocateImageId(docProperties.Id);
                }

                // Write token
                context.Writer.WriteToken(token);

                threadGovernor.Yield();
            }
        }
Example #3
0
        /// <summary>
        /// Main processing method to convert an XML tree to a tree of instructions.
        /// Starting at the root we do the following:
        /// - read the token stream
        /// - sometimes automatically ignore tokens
        /// - sometimes implicitly pop off an instruction if we hit an 'implicit end' marker
        /// - buffer tokens
        /// - process the tokens
        /// </summary>
        /// <param name="root"></param>
        /// <returns></returns>
        public TemplateData BuildInstructionTree(OpenXmlElement root)
        {
            if (_templateData != null)
            {
                throw new InvalidOperationException("BuildInstructionTree can only be called once per instance of OpenXmlReader.");
            }

            _templateData = new TemplateData();
            ReaderContext.OpenXmlReader = this;

            // Initialize instruction stack
            _templateData.RootInstruction = CreateRootInstruction();
            _instructionStack.Push(_templateData.RootInstruction);

            // Initialize token reader
            IEnumerable <Token> tokens = ReadTokens(root);

            ThreadCpuGovernor threadGovernor = new ThreadCpuGovernor(50);

            // Main reader loop
            foreach (Token token in tokens)
            {
                ProcessTokenRaw(token);

                if (_discardUntil != null)
                {
                    if (token == _discardUntil)
                    {
                        _discardUntil = null;
                    }
                    continue;
                }

                while (token == CurrentInstruction.ImplicitlyEndBefore)
                {
                    ProcessEndImplicit();
                }

                _tokenBuffer.Add(token);
                ProcessToken(token);

                while (token == CurrentInstruction.ImplicitlyEndAfter)
                {
                    ProcessEndImplicit();
                }

                threadGovernor.Yield();
            }

            // Handle final block

            FlushTokenBufferToDataInstruction();

            // Verify and return root instruction.
            _instructionStack.Pop();
            if (_instructionStack.Count > 0)
            {
                throw new Exception("Expected empty instruction stack.");
            }

            return(_templateData);
        }