microsoft / microsoft/vs-threading

VSTHRD010 triggers on method defining delegates that require the main thread

Open
#1,358 0 comments 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

It seems like VSTHRD010 will flag methods that define delegates that call ThreadHelper.ThrowIfNotOnUIThread, though the method itself does not invoke the delegate.

Repro steps
abstract class Foo
{
    Foo()
    {
        this.RefreshCommand = new RelayCommand(
            execute: (object param) => { ThreadHelper.ThrowIfNotOnUIThread(); StartForceRefreshRequest(param); }
            canExecute: (_) => { ThreadHelper.ThrowIfNotOnUIThread(); return CanExecuteForceRefreshCommand(); }
        );
    }
}

class Bar : Foo
{
    Bar() : base()
    {
        // analyzer flags this constructor with the warning:
        // VSTHRD010: Accessing "Foo..ctor" should only be done on the main thread
    }
}
Expected behavior

No threading analyzer warning for the described case.

Actual behavior

Threading analyzer fires warning.

  • Version used: latest (v17.11.20)
Additional context

I have not extensively verified the exact conditions for this, but I have verified that removing the thread-affinitized delegate prevents the analyzer warning.

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 the VSTHRD010 warning from the constructor and delegate example in the issue, then trace the analyzer's handling of thread-affinitized delegate bodies. Done means the constructor no longer receives a warning when it only defines delegates that call ThreadHelper.ThrowIfNotOnUIThread, while genuine main-thread access remains diagnosed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.