public void TestWriteReadLock_Exception() { string testpath = Path.GetRandomFileName(); Directory.CreateDirectory(testpath); IRWLock testlock = RWLockFactory.Create(testpath); try { string e = Environment.CurrentDirectory; testlock.WriteLock(); testlock.ReadLock(); } catch (ReadLockTimeoutException) { Assert.Pass(); } finally { testlock.ReadUnlock(); testlock.WriteUnlock(); } Directory.Delete(testpath, true); Assert.Fail(); }
public void TestUserReadLock_Ok() { string testpath = Path.GetRandomFileName(); Directory.CreateDirectory(testpath); IRWLock wlock = RWLockFactory.Create(testpath); using (RWLockFactory.CreateUsingReadLock(testpath)) { //was auch immer der Read tun will } //Writer sollte (wieder) erlaubt sein try { wlock.WriteLock(); } catch (WriteLockTimeoutException) { Assert.Fail(); } finally { wlock.WriteUnlock(); Directory.Delete(testpath, true); } Assert.Pass(); }
public static string GetToken(string secret) { TkDebug.AssertArgumentNullOrEmpty(secret, "secret", null); IRWLock rwLock = BaseGlobalVariable.Current.CreateRWLock(); CorpAccessToken token; using (rwLock) { if (fCurrent == null) { token = RWLock.WriteLockAction(rwLock, () => { CorpAccessToken result = CorpAccessToken.CreateToken(secret); fCurrent = new CorpAccessTokenList(); fCurrent.AddToken(result); SaveToken(); return(result); }); return(token.Token); } else { token = RWLock.ReadLockAction(rwLock, () => { CorpAccessToken result = fCurrent.CorpTokens[secret]; if (result == null || result.IsExpire) { result = RWLock.WriteLockAction(rwLock, () => { CorpAccessToken newToken = CorpAccessToken.CreateToken(secret); fCurrent.CorpTokens.AddOrReplace(newToken); SaveToken(); return(newToken); }); } return(result); }); return(token.Token); } } }
public UseReadLock(string path) { _rwlock = RWLockFactory.Create(path); _rwlock.ReadLock(); }
public UseWriteLock(string path) { _rwlock = RWLockFactory.Create(path); _rwlock.WriteLock(); }