public static Timeout RunMax(TimeSpan timeSpan, LockingThreadTimeoutDelegate lockingThreadTimeoutDelegate) { Timeout toReturn = new Timeout(); toReturn.Thread = Thread.CurrentThread; toReturn.LockingThreadTimeoutDelegate = lockingThreadTimeoutDelegate; toReturn.Timer = new Timer(toReturn.HandleTimeout, null, Convert.ToInt32(timeSpan.TotalMilliseconds), System.Threading.Timeout.Infinite); return toReturn; }
private static TimedLock CreateLock(object o, TimeSpan timeout, TimeSpan? aquiredLockTimeout, LockingThreadTimeoutDelegate lockingThreadTimeoutDelegate) { TimedLock toReturn = new TimedLock(); toReturn.Thread = Thread.CurrentThread; toReturn.target = o; //string lockInformation = string.Format( // "\n\n\nThreadID: {0}\ntype of lock object:{2},Call stack: {2}\n\n\n", // Thread.CurrentThread.ManagedThreadId, // o.GetType().FullName, // Environment.StackTrace); if (!Monitor.TryEnter(o, timeout)) { //string lockHolderInformation; //lock (LockHolders) // if (!LockHolders.TryGetValue(o, out lockHolderInformation)) // lockHolderInformation = "not available"; //throw new LockTimeoutException(o, lockHolderInformation); throw new LockTimeoutException(o); } //lock (LockHolders) // LockHolders[o] = lockInformation; toReturn.myLockingThreadTimeoutDelegate = lockingThreadTimeoutDelegate; if (null != aquiredLockTimeout) toReturn.Timer = new Timer(toReturn.Timeout, null, Convert.ToInt32(aquiredLockTimeout.Value.TotalMilliseconds), -1); else toReturn.Timer = null; /*#if DEBUG toReturn.ID = Guid.NewGuid(); OnLockCreated(toReturn); #endif*/ return toReturn; }
public static TimedLock Lock(object o, TimeSpan timeout, TimeSpan lockAquiredTimeout, LockingThreadTimeoutDelegate lockingThreadTimeoutDelegate) { return CreateLock(o, timeout, lockAquiredTimeout, lockingThreadTimeoutDelegate); }