using System; namespace JiShe.CollectBus.Common.Extensions { /// /// Extension methods to make locking easier. /// public static class LockExtensions { /// /// Executes given by locking given object. /// /// Source object (to be locked) /// Action (to be executed) public static void Locking(this object source, Action action) { lock (source) { action(); } } /// /// Executes given by locking given object. /// /// Type of the object (to be locked) /// Source object (to be locked) /// Action (to be executed) public static void Locking(this T source, Action action) where T : class { lock (source) { action(source); } } /// /// Executes given and returns it's value by locking given object. /// /// Return type /// Source object (to be locked) /// Function (to be executed) /// Return value of the public static TResult Locking(this object source, Func func) { lock (source) { return func(); } } /// /// Executes given and returns it's value by locking given object. /// /// Type of the object (to be locked) /// Return type /// Source object (to be locked) /// Function (to be executed) /// Return value of the public static TResult Locking(this T source, Func func) where T : class { lock (source) { return func(source); } } } }