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 triggers on method defining delegates that require the main thread #1358

Open
asundheim opened this issue Oct 16, 2024 · 0 comments

Comments

@asundheim
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant