Skip to content

VSTHRD010 triggers on method defining delegates that require the main thread #1358

@asundheim

Description

@asundheim

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.

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