public void DoStufOnFile(string fileName) { using (ILocker locker = this.provider.GetLockObjectForKey(fileName)) { // A using statement is not as 'strong' as a lock, so // we need to call Enter within the using statement. locker.Enter(); // Long running operation on file here. } }
public void ThreadX() { long res = 0; for (int i = 0; i < 10000; i++) { _locker.Enter(); Interlocked.Increment(ref concurrent); for (int z = 0; z < 1000; z++) { res += res; } if (concurrent > 1) { Console.WriteLine("{0} concurrency issues!", _locker.GetType().Name); } Interlocked.Decrement(ref concurrent); _locker.Exit(); } }