简体   繁体   中英

“This function requires all threads to evaluate” after `new AdhocWorkspace()` within VS custom visualizer

I am trying to write a visualizer that maps expressions in an expression tree to Roslyn syntax nodes , in order to generate code for the expression tree. Part of the syntax tree generation is a call to the AdhocWorkspace constructor.

When I run the visualizer using the VisualizerDevelopmentHost , everything works just fine:

using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Linq.Expressions;

namespace _testVisualizer {
    class Program {
        [STAThread]
        static void Main(string[] args) {
            Expression<Func<bool>> expr = () => true;
            var data = new TestVisualizerData(expr);
            var visualizerHost = new VisualizerDevelopmentHost(data, typeof(TestVisualizer));
            visualizerHost.ShowVisualizer();

            Console.ReadKey(true);
        }
    }
}

But when I try to use the visualizer through the Visual Studio UI (by hovering over expr , clicking on the magnifying glass icon and choosing my visualizer), I get the following message:

Unable to perform function evaluation on the process being debugged.

Additional information

The function evaluation requires all threads to run.

I've identified the following as triggering the error:

workspace = new AdhocWorkspace();

which assigns to the workspace field on my Mapper class ( source ).

Why does calling the AdhocWorkspace constructor trigger this warning? How can I work around this?


This is an MCVE that demonstrates the issue:

using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;

[assembly: DebuggerVisualizer(typeof(_testVisualizer.TestVisualizer), typeof(_testVisualizer.TestVisualizerDataObjectSource), Target = typeof(System.Linq.Expressions.Expression), Description = "Test Visualizer")]

namespace _testVisualizer {
    public class TestVisualizer : DialogDebuggerVisualizer {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) {
            var data = (TestVisualizerData)objectProvider.GetObject();
            var txt = new TextBlock();
            txt.SetBinding(TextBlock.TextProperty, "Status");
            var window = new Window {
                DataContext = data,
                Content = txt
            };
            window.ShowDialog();
        }
    }

    [Serializable]
    public class TestVisualizerData {
        public TestVisualizerData() { }
        public TestVisualizerData(System.Linq.Expressions.Expression expr) {
            var workspace = new AdhocWorkspace();
            Status = "Success";
        }
        public string Status { get; set; }
    }

    public class TestVisualizerDataObjectSource : VisualizerObjectSource {
        public override void GetData(object target, Stream outgoingData) {
            var expr = (System.Linq.Expressions.Expression)target;
            var data = new TestVisualizerData(expr);
            base.GetData(data, outgoingData);
        }
    }
}

Perhaps the AdhocWorkspace alternate constructor could be leveraged to use the SyntaxNode API in a single thread.

I've filed an issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM