c# – 为什么表达式总是为’双重检查锁定’?

前端之家收集整理的这篇文章主要介绍了c# – 为什么表达式总是为’双重检查锁定’?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有单独的对象’服务’和两个初始化和释放它的方法
public class BaseService
{
    protected static readonly object StaticLockObject = new object();
}

public abstract class WebServiceBase<TService> : BaseService
    where TService : System.Web.Services.Protocols.SoapHttpClientProtocol,new()
{
    protected static void EnsureServiceIsOpened()
    {
        if (Service == null)
        {
            lock (StaticLockObject)
            {
                if (Service == null)
                {
                    Service = new TService();
                }
            }
        }
    }

    protected static void EnsureServiceIsClosed()
    {
        if (Service != null)
        {
            lock (StaticLockObject)
            {
                if (Service != null) // Why expression is always true
                {
                    Service.Dispose();
                    Service = null;
                }
            }
        }
    }

对于带注释resharper的行(我使用版本5.1)显示一个提到的警告…

问题1:为什么?

问题2:为什么它不在’EnsureServiceIsOpened’方法显示“类似”消息?

谢谢.

解决方法

这是ReSharper 5.X代码分析引擎中的错误.
在ReSharper 6.0中修复.

顺便说一下,R​​eSharper 6带来了更多双锁模式分析的东西:)

原文链接:https://www.f2er.com/csharp/244228.html

猜你在找的C#相关文章