C# 使用 Monitor.IsEntered() 方法来检查锁的状态。如果该方法返回 true,则表示当前线程已经获取了该锁;如果返回 false,则表示该锁没有被当前线程获取。
例如:
object lockObject = new object();
if (Monitor.IsEntered(lockObject))
{
Console.WriteLine("The lock is acquired by the current thread.");
}
else
{
Console.WriteLine("The lock is not acquired by the current thread.");
}