I have a lock in my code.
I have two threads running at the same time.
How can I tell if a thread is locking that object?
private readonly object _lockObject = new Object();
// Both methods running
public void Method1()
{
if(certainCriteria)
{
lock(_lockObject)
{
//doWork;
}
}
}
// Both methods running
public void Method2()
{
if( isLocked?(_lockObject))
{
//doWork;
}
}
Has anyone got the isLocked? method?
Thanks in advance!
You could use Monitor.TryEnter
(either with a timeout of 0, or the overload which doesn't take a timeout at all) and then immediately call Monitor.Exit
if it succeeds - but I'd say this is generally a bad design smell. In particular, the data is stale immediately you return it.
What are you trying to achieve?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With