replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
private static object _lock = new object();
public static void ExecuteLock(string key, Action action)
{
try
{
bool isFree = true;
do
{
lock (_lock)
{
if (!_locksByKeys.ContainsKey(key))
_locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
isFree = Monitor.TryEnter(_locksByKeys[key]);
}
if (!isFree)
{
Thread.Sleep(50);
}
} while (!isFree);
action.Invoke();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Monitor.Exit(_locksByKeys[key]);
}
}
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
private static object _lock = new object();
public static void ExecuteLock(string key, Action action)
{
try
{
bool isFree = true;
do
{
lock (_lock)
{
if (!_locksByKeys.ContainsKey(key))
_locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
isFree = Monitor.TryEnter(_locksByKeys[key]);
}
if (!isFree)
{
Thread.Sleep(50);
}
} while (!isFree);
action.Invoke();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Monitor.Exit(_locksByKeys[key]);
}
}
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
private static object _lock = new object();
public static void ExecuteLock(string key, Action action)
{
try
{
bool isFree = true;
do
{
lock (_lock)
{
if (!_locksByKeys.ContainsKey(key))
_locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
isFree = Monitor.TryEnter(_locksByKeys[key]);
}
if (!isFree)
{
Thread.Sleep(50);
}
} while (!isFree);
action.Invoke();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Monitor.Exit(_locksByKeys[key]);
}
}
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
private static object _lock = new object();
public static void ExecuteLock(string key, Action action)
{
try
{
bool isFree = true;
do
{
lock (_lock)
{
if (!_locksByKeys.ContainsKey(key))
_locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
isFree = Monitor.TryEnter(_locksByKeys[key]);
}
if (!isFree)
{
Thread.Sleep(50);
}
} while (!isFree);
action.Invoke();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Monitor.Exit(_locksByKeys[key]);
}
}
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
private static object _lock = new object();
public static void ExecuteLock(string key, Action action)
{
try
{
bool isFree = true;
do
{
lock (_lock)
{
if (!_locksByKeys.ContainsKey(key))
_locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
isFree = Monitor.TryEnter(_locksByKeys[key]);
}
if (!isFree)
{
Thread.Sleep(50);
}
} while (!isFree);
action.Invoke();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Monitor.Exit(_locksByKeys[key]);
}
}
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
//Implementation of the class who will block to two process don't execute if they have the same key.
public class DynamicLockGenerator
{
private static Dictionary<string, object> _locksByKeys = new Dictionary<string, object>();
private static object _lock = new object();
public static void ExecuteLock(string key, Action action)
{
try
{
bool isFree = true;
do
{
lock (_lock)
{
if (!_locksByKeys.ContainsKey(key))
_locksByKeys.Add(key, new GuidLock(Guid.NewGuid()));
isFree = Monitor.TryEnter(_locksByKeys[key]);
}
if (!isFree)
{
Thread.Sleep(50);
}
} while (!isFree);
action.Invoke();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Monitor.Exit(_locksByKeys[key]);
}
}
}
//This is the method call.
DynamicLockGenerator.ExecuteLock(key, () => SomeMethodToExecuteAlone());
Improved grammar/readability of intro, removed extra(8) whitespaces from code formatting
forsvarir
- 11.8k
- 7
- 39
- 72
Loading
Loading
lang-cs