Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tonyhallett opened this issue Mar 1, 2022 · 1 comment
Open

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

tonyhallett opened this issue Mar 1, 2022 · 1 comment

Comments

@tonyhallett
Copy link

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.

@AArnott
Copy link
Member

AArnott commented Mar 9, 2022

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
Projects
None yet
Development

No branches or pull requests

2 participants