Skip to content

VSTHRD010 for callers of method that switches to main thread #993

Open

Description

Bug description

Callers to this method have warning VSTHRD010

        private void UpdateColoursFromFontsAndColors()
        {
            ThreadHelper.JoinableTaskFactory.Run(async () =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                var success = fontAndColorStorage.OpenCategory(ref categoryWithCoverage, storeFlags);
                if (success == VSConstants.S_OK)
                {
                    CoverageTouchedArea = GetColor("Coverage Touched Area");
                    CoverageNotTouchedArea = GetColor("Coverage Not Touched Area");
                    CoveragePartiallyTouchedArea = GetColor("Coverage Partially Touched Area");
                }
                fontAndColorStorage.CloseCategory();
                //throw ?
                requiresFromFontsAndColours = false;
            });
            
        }

        private System.Windows.Media.Color GetColor(string displayName)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var touchAreaInfo = new ColorableItemInfo[1];
            var getItemSuccess = fontAndColorStorage.GetItem(displayName, touchAreaInfo);
            if (getItemSuccess == VSConstants.S_OK)
            {
                return ParseColor(touchAreaInfo[0].crBackground);
            }
            throw new Exception("Failed to get color");
        }

Remove the calls to GetColor and no warnings.
Make GetColor a local function ( without ThreadHelper.ThrowIfNotOnUIThread() ) and no warnings

Expected behavior

No warnings

Actual behavior

warnings

  • Version used: Microsoft.VisualStudio.SDK - 17.0.31902.203
  • Application (if applicable):

Additional context

Add any other context about the problem here.

Activity

  1. AArnott commented on Mar 9, 2022

    @AArnott
    Member

    Thank you for reporting. Looking at the report, I agree no warning should be produced here.
    Until this is fixed (and we'd welcome a PR that includes a new test), you might be able to work around it by moving the content of your JTF.Run into its own async method. Then call that async method as the only step inside your JTF.Run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions