C# - ReSharper 9.x用新警告标记代码 - “字段上的同步不一致”

时间:2015-11-12 22:08:21

标签: c# resharper

以下运行RS 8.2.x检查的代码没有问题;在对RS 9.x进行检查后,RS发出此警告:“该字段有时在同步块内使用,有时在没有同步的情况下使用。”

// The wait time determines if we block on trying to acquire the lock.
int waitTime = lockQueue ? Timeout.Infinite : 0;

Queue<EventArgs> fredEventQueueCopy = null;

if(System.Threading.Monitor.TryEnter(fredEventQueueLocker, waitTime))
{
    try
    {
        if(fredEventQueue.Count > 0)
        {
            fredEventQueueCopy = fredEventQueue;
            fredEventQueue = new Queue<EventArgs>();
        }
    }
    finally
    {
        System.Threading.Monitor.Exit(fredEventQueueLocker);
    }
}

“fredEventQueue”代码中其他地方的所有访问都只是“锁定”;事实上,如果我用下面的代码替换上面的代码,RS 9.x不会标记此警告:

// The wait time determines if we block on trying to acquire the lock.
int waitTime = lockQueue ? Timeout.Infinite : 0;
Queue<EventArgs> fredEventQueueCopy = null;

lock(fredEventQueueLocker)
{
    if(fredEventQueue.Count > 0)
    {
        fredEventQueueCopy = fredEventQueue;
        fredEventQueue = new Queue<EventArgs>();
    }
}

为什么RS 9.x会抛出这个新的检查警告?

1 个答案:

答案 0 :(得分:4)

似乎ReSharper并不解释Monitor.TryEnter / Monitor.Exit模式等于lock语句。对我来说,这似乎是一种误报。

实际上这是RSRP-441222,据报道在9.2中已修复。