日期:2014-05-18  浏览次数:20438 次

请问lock(obj)和Application.Lock有什么区别?
C# code

                int a = 0;
                lock (Application["delDatetime"])
                {
                    a += 1;
                    Application["delDatetime"] = System.DateTime.Now;
                }



C# code

                int a = 0;
                Application.Lock();
                a += 1;
                Application["delDatetime"] = System.DateTime.Now;
                Application.UnLock();


请问在以上两段代码遇到并发的时候,是不是都能锁住a += 1?

------解决方案--------------------
看了下源码 事实 Application.Lock()的底层也是用 lock来实现的
C# code

internal virtual void AcquireWrite()
{
    lock (this)
    {
        while (this._lock != 0)
        {
            try
            {
                Monitor.Wait(this);
                continue;
            }
            catch (ThreadInterruptedException)
            {
                continue;
            }
        }
        this._lock = -1;
    }
}