none
Creating DocumentTask error: unable to get IVsCategorizedFontColorPrefs

    Question

  • Hi,

    I am writing a language service using MPF in VS2010, and trying to use Source.CreateErrorTaskItem() to log compile messages to Error List. However, this method sometimes works, and sometimes doesn't by throwing the an InvalidOperationException: Unable to get IVsCategorizedFontColorPrefs from buffer's text manager.

    I use the following steps to replicate both the working and the unworking scenario:

    1. manually close all opened documents in the VS
    2. build/rebuild my custom project. Since the project contains syntax errors, my compiler will try to log those by invoking the code below in sequence:
    3. _languageService.OpenDocument(filepath);
    4. var src = _languageService.GetSource(filepath);
    5. var task = src.CreateErrorTaskItem(...)

    After everything is done, the document with errors is opened, and the errors are identified with blue squiggles (another question: WHY BLUE?). If I then close this document, and open it again, the compiler will be reinvoked since a ParseRequest.Check is issued. But this time, the step 5 will FAIL with the aforementioned exeption. Why? Furthermore, the opened document will not have any squiggles, although the errors are still sent to the Error List.

     

    Thanks in advance.

    Mark

    P.S. I have seen people getting the same problem with VS2010 Beta2 or RC, but I'm using VS2010 Premium.

    Thursday, September 23, 2010 2:38 AM

Answers

  • Hi Mark,

    I suspect this is thrown from the following code in our code editor implementation:

     public FONTCOLORPREFERENCES2 GetFontAndColorPreferences()
     {
      FONTCOLORPREFERENCES2 fcprefsData = new FONTCOLORPREFERENCES2();
     
      // Use the text manager to get a color table
      IVsCategorizedFontColorPrefs fcprefs = Common.GetService(this._serviceProvider, typeof(SVsTextManager).GUID, true) as IVsCategorizedFontColorPrefs;
      if (fcprefs == null)
      throw new InvalidOperationException("Unable to get IVsCategorizedFontColorPrefs from buffer's text manager");
    

    When trying to call IVsFontsAndColorsInformation.GetFontAndColorPreferences(). I'm not certain why the GetService would fail to retrieve the interface though. If you configure the debugger to break when an InvalidOperationException is thrown, what does the callstack look like? Are you by chance attempting to invoke this off a background thread?

    Thanks,


    Ed Dore
    Monday, November 01, 2010 8:05 PM
  • Hi Ed,

    Finally got time to get back to this issue.

    I can now confirm that the failed scenario only occurs on the background thread: Parse Thread, and is consistently replicatable. Once I use Microsoft.VisualStudio.Shell.ThreadHelper.Generic.Invoke to dispatch it to the Main Thread, everything works great! I guess Visual Studio is managing multiple threads internally, it's easy to forget to do the dispatch :)

    Thank you very much for pointing me at the right direction.


    Mark @markgu http://markyourfootsteps.wordpress.com
    • Marked as answer by Mark Gu Monday, November 08, 2010 8:50 PM
    Monday, November 08, 2010 8:48 PM

All replies

  • Hi Mark,

    I suspect this is thrown from the following code in our code editor implementation:

     public FONTCOLORPREFERENCES2 GetFontAndColorPreferences()
     {
      FONTCOLORPREFERENCES2 fcprefsData = new FONTCOLORPREFERENCES2();
     
      // Use the text manager to get a color table
      IVsCategorizedFontColorPrefs fcprefs = Common.GetService(this._serviceProvider, typeof(SVsTextManager).GUID, true) as IVsCategorizedFontColorPrefs;
      if (fcprefs == null)
      throw new InvalidOperationException("Unable to get IVsCategorizedFontColorPrefs from buffer's text manager");
    

    When trying to call IVsFontsAndColorsInformation.GetFontAndColorPreferences(). I'm not certain why the GetService would fail to retrieve the interface though. If you configure the debugger to break when an InvalidOperationException is thrown, what does the callstack look like? Are you by chance attempting to invoke this off a background thread?

    Thanks,


    Ed Dore
    Monday, November 01, 2010 8:05 PM
  • Sorry about the late reply, didn't get any alert...

    As the problem was logged a while ago, and since we have moved to a different direction, I need a little bit time to re-setup the environment and provide you more information. In the meantime, I will also try to invoke the method in a background thread as you suggested.

    Thanks


    Mark @markgu http://markyourfootsteps.wordpress.com
    Thursday, November 04, 2010 9:59 AM
  • Hi Mark,

    Invoking the method on a background thread wasn't a suggestion. I've seen problems in the past where interfaces aren't properly marshalled to another thread. I was just curious as to whether or not you were invoking this method in the main UI thread, or some other thread.

    Thanks,


    Ed Dore
    Thursday, November 04, 2010 6:21 PM
  • Hi Ed,

    I thought so as well. But still want to try it to see if I can consistently replicate the failed scenario.

    Thanks


    Mark @markgu http://markyourfootsteps.wordpress.com
    Thursday, November 04, 2010 7:02 PM
  • Hi All

    I am receiving the same exception constantly in the following 

     

    var filePath = "<ANY>.txt";

     

            var serviceProvider = 

                    new ServiceProvider(

                        (IOleServiceProvider)(DTE2)System.Activator.CreateInstance(

                            System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0"), 

                            true));

     

                IVsUIHierarchy hierarchy;

                uint itemID;

                IVsWindowFrame windowFrame;

                VsShellUtilities.OpenDocument(serviceProvider, filePath, Guid.Empty, out hierarchy, out itemID, out windowFrame);

     

                var textView = VsShellUtilities.GetTextView(windowFrame);

     

                IVsTextLines textLines;

                textView.GetBuffer(out textLines);

     

                var textSpan = new TextSpan()

                {

                    iStartLine = 1,

                    iStartIndex = 1,

                    iEndLine = 2,

                    iEndIndex = 2

                };

     

                var documentTask =

                    new DocumentTask(

                        serviceProvider,

                        textLines,

                        MARKERTYPE.MARKER_BOOKMARK,

                        textSpan,

                        filePath);

     

    Sunday, November 07, 2010 10:27 AM
  • Your issue falls into a category that we are not able to resolve using the forums. There are various

    support options such as advisory and per issue. Please visit the below link to see the various paid

    support options that are available to better meet your needs.

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Alliance and Premier VSIP membership includes a complimentary MSDN subscription, which includes 4

    professional support incidents. These can be used to initialize a support request with Microsoft's

    Customer Support Services. Some versions of Visual Studio include a number of free support incidents

    as well. See the "Technical Support Incidents" topic for details.


    bill boyce
    Monday, November 08, 2010 5:11 PM
  • Hi Ed,

    Finally got time to get back to this issue.

    I can now confirm that the failed scenario only occurs on the background thread: Parse Thread, and is consistently replicatable. Once I use Microsoft.VisualStudio.Shell.ThreadHelper.Generic.Invoke to dispatch it to the Main Thread, everything works great! I guess Visual Studio is managing multiple threads internally, it's easy to forget to do the dispatch :)

    Thank you very much for pointing me at the right direction.


    Mark @markgu http://markyourfootsteps.wordpress.com
    • Marked as answer by Mark Gu Monday, November 08, 2010 8:50 PM
    Monday, November 08, 2010 8:48 PM