From ddb6bffe251bacdc9a1f84a088662feee51e0444 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 2 Apr 2019 19:45:53 -0400 Subject: [PATCH] Fixes #101 --- .../Abstracts/SynchronizedLifetimeManager.cs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs b/src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs index 8211854c..25dbca74 100644 --- a/src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs +++ b/src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs @@ -24,20 +24,31 @@ public abstract class SynchronizedLifetimeManager : LifetimeManager, IDisposable { #region Fields - private readonly object _lockObj = new object(); + private readonly object _lock = new object(); + + /// + /// This field controlls how long the monitor will wait to + /// enter the lock. It is by default or number of + /// milliseconds from 0 to 2147483647. + /// + public static int ResolveTimeout = Timeout.Infinite; #endregion /// public override object GetValue(ILifetimeContainer container = null) { - Monitor.Enter(_lockObj); - var result = SynchronizedGetValue(container); - if (NoValue != result) + if (Monitor.TryEnter(_lock, ResolveTimeout)) { - Monitor.Exit(_lockObj); + var result = SynchronizedGetValue(container); + if (NoValue != result) + { + Monitor.Exit(_lock); + } + return result; } - return result; + else + throw new TimeoutException($"Failed to enter a monitor"); } /// @@ -85,11 +96,11 @@ protected virtual void TryExit() { #if !NET40 // Prevent first chance exception when abandoning a lock that has not been entered - if (!Monitor.IsEntered(_lockObj)) return; + if (!Monitor.IsEntered(_lock)) return; #endif try { - Monitor.Exit(_lockObj); + Monitor.Exit(_lock); } catch (SynchronizationLockException) {