一般尝试获取锁的方法
public virtual bool Obtain(long lockWaitTimeout)
{
bool locked = Obtain();
int maxSleepCount = (int) (lockWaitTimeout / LOCK_POLL_INTERVAL);
int sleepCount = 0;
//尝试在Timeout时间内获取锁
while (!locked)
{
if (++sleepCount == maxSleepCount)
{
throw new System.IO.IOException("Lock obtain timed out: " + this.ToString());
}
try
{
//1ms 数是10000 ticks ,1ms*1000=1s
System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * LOCK_POLL_INTERVAL));
}
catch (System.Threading.ThreadInterruptedException e)
{
throw new System.IO.IOException(e.ToString());
}
locked = Obtain();
}
return locked;
}