Reputation: 24067
I have a project and a class library.
I need the class library to update storage items. In my project I need to access these storage items. Can I use lock
on the same instance from different projects and will this work?
Upvotes: 3
Views: 225
Reputation: 1502076
So long as you're locking on genuinely the same object, that should work absolutely fine. If you were using different AppDomains things would get more complicated, but if it's just (say) both Project A and Project B locking on an object which originally came from Project C, that shouldn't be a problem.
At least, it'll work technically. Personally I usually prefer to keep locks as private as possible - for example, rather than locking on this
or a reference obtained from elsewhere, I'll often create an object whose sole purpose is locking:
public class Foo
{
private readonly object mutex = new object();
...
}
That way I know that the only code which can acquire that lock is code in Foo
. It makes the locks easier to reason about.
Upvotes: 6