public static BackgroundParseTask StartNew(RazorTemplateEngine engine, string sourceFileName, TextChange change)
        {
            BackgroundParseTask task = new BackgroundParseTask(engine, sourceFileName, change);

            task.Start();
            return(task);
        }
 public static BackgroundParseTask StartNew(RazorTemplateEngine engine, string sourceFileName, TextChange change)
 {
     BackgroundParseTask task = new BackgroundParseTask(engine, sourceFileName, change);
     task.Start();
     return task;
 }
        private void OnParseCompleted(GeneratorResults results, BackgroundParseTask parseTask)
        {
            try
            {
                // Lock the state objects
                bool treeStructureChanged = true;
                TextChange lastChange;
                lock (_lock)
                {
                    // Are we still active?
                    if (_outstandingParserTasks.Count == 0 || !ReferenceEquals(parseTask, _outstandingParserTasks.Peek()))
                    {
                        // We aren't, just abort
                        return;
                    }

                    // Ok, we're active. Flush out the changes from all the parser tasks and clear the stack of outstanding parse tasks
                    TextChange[] changes = _outstandingParserTasks.Select(t => t.Change).Reverse().ToArray();
                    lastChange = changes.Last();
                    _outstandingParserTasks.Clear();

                    // Take the current tree and check for differences
                    treeStructureChanged = CurrentParseTree == null || TreesAreDifferent(CurrentParseTree, results.Document, changes);
                    CurrentParseTree = results.Document;
#if DEBUG
                    _currentCompileUnit = results.GeneratedCode;
#endif
                    _lastChangeOwner = null;
                }

                // Done, now exit the lock and fire the event
                OnDocumentParseComplete(new DocumentParseCompleteEventArgs()
                {
                    GeneratorResults = results,
                    SourceChange = lastChange,
                    TreeStructureChanged = treeStructureChanged
                });
            }
            finally
            {
                parseTask.Dispose();
            }
        }