lock
on local variables is error-prone CS-W1094Trying to acquire a lock on a local variable is error-prone as different threads may end up locking on different instances of the same variable.
Instead, you should be locking on a class field that is preferably designated as private
and readonly
, i.e. a dedicated object.
Microsoft guidelines explicitly state that you should avoid locking on:
1. this
,
System.Type
instances, and,
string
-s and literals.
It is recommended that you fix this issue as soon as possible to avoid race conditions that undermine the synchronicity of your application.