// timeout最大等待时间,超出即超时 // onEnter要执行的函数 public bool WaitForTime(TimeSpan timeout, Func <bool> onEnter) { var temp = timeout; DateTime nowTime = DateTime.Now; PerWaitNum type = TryEnter(onEnter); while (type != PerWaitNum.SuccessAndExits) { if (!WaitTime(ref timeout, ref nowTime)) { break; } type = TryEnter(onEnter); } return(type == PerWaitNum.SuccessAndExits); }
private PerWaitNum TryEnter(Func <bool> onEnter) { bool success = asyncObject.Lock(); if (success) { PerWaitNum r = PerWaitNum.SuccessAndContinue; if (onEnter()) { r = PerWaitNum.SuccessAndExits; } asyncObject.UnLock(); return(r); } return(PerWaitNum.Fail); }