microsoft / microsoft/vs-threading

VSTHRD010 for callers of method that switches to main thread

Open
#993 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

analyzers bug
Dominant language
C#
Stars
1k
Forks
160
Avg merge
1d 12h
Merged PRs (30d)
28

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing VSTHRD010 with the UpdateColoursFromFontsAndColors and GetColor methods shown in the issue. Trace how the analyzer handles the JoinableTaskFactory.Run call, SwitchToMainThreadAsync, and the local-function variant, then verify that the reported caller warnings are eliminated without introducing new warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.