vs-threading
vs-threading copied to clipboard
VSTHRD010 triggers on method defining delegates that require the main thread
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.