这是关于添加一个对像到一个缓存中的方式
我对这段代码的疑问是
既然已用了Lock使这段代码成为临界区,为什么用要lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock)请高手解我心中疑问! CacheItem cacheItemBeforeLock = null;
bool lockWasSuccessful = false; do
{
lock (inMemoryCache.SyncRoot)
{
if (inMemoryCache.Contains(key) == false)
{
cacheItemBeforeLock = new CacheItem(key, addInProgressFlag, CacheItemPriority.NotRemovable, null);
inMemoryCache[key] = cacheItemBeforeLock;
}
else
{
cacheItemBeforeLock = (CacheItem)inMemoryCache[key];
} lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
} if (lockWasSuccessful == false)
{
Thread.Sleep(0);
}
} while (lockWasSuccessful == false);
我对这段代码的疑问是
既然已用了Lock使这段代码成为临界区,为什么用要lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock)请高手解我心中疑问! CacheItem cacheItemBeforeLock = null;
bool lockWasSuccessful = false; do
{
lock (inMemoryCache.SyncRoot)
{
if (inMemoryCache.Contains(key) == false)
{
cacheItemBeforeLock = new CacheItem(key, addInProgressFlag, CacheItemPriority.NotRemovable, null);
inMemoryCache[key] = cacheItemBeforeLock;
}
else
{
cacheItemBeforeLock = (CacheItem)inMemoryCache[key];
} lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
} if (lockWasSuccessful == false)
{
Thread.Sleep(0);
}
} while (lockWasSuccessful == false);
解决方案 »
- 未将对象引用设置到对象的实例
- csc找不到源文件
- 类似VS clickonce上传时在远程电脑上创建文件夹,需要什么技术
- 截取字符串中html的链接地址!!!!!!!!!!!!!!!!!!!!!1用c#怎么写!!!!!!!!!!!!!!!!!
- 求此生成编号的优化代码
- C#openfileDialog函数打开的txt文件乱码
- 虚心请教一个菜鸟问题:如何给第一张图片也要加个<a></a>
- 怎么样将label控件的文本加一个点划线的框
- 有意思的问题。
- C# npoi 怎么获取自动换行以后的 行高?
- 谁可以提供三层项目(cs)给我,要求用remoting实现的中间层;
- datagrid的打印!请大家帮忙!
zhongkeruanjian.cnblogs.com