相关疑难解决方法(0)

为什么使用try {} finally {}和一个空的try块?

我注意到在System.Threading.TimerBase.Dipose()方法中有一个try{} finally{}块但是try{}是空的.

使用try{} finally{}空尝试有什么价值吗?

http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.Threading&type=TimerBase

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
internal bool Dispose(WaitHandle notifyObject)
{
    bool status = false;
    bool bLockTaken = false;
    RuntimeHelpers.PrepareConstrainedRegions();
    try {
    }
    finally {
        do {
            if (Interlocked.CompareExchange(ref m_lock, 1, 0) == 0) {
                bLockTaken = true;
                try {
                    status = DeleteTimerNative(notifyObject.SafeWaitHandle);
                }
                finally {
                    m_lock = 0;
                }
            }
            Thread.SpinWait(1);
            // yield to processor
        }
        while (!bLockTaken);
        GC.SuppressFinalize(this);
    }

    return status;
}
Run Code Online (Sandbox Code Playgroud)

.net c#

235
推荐指数
2
解决办法
7302
查看次数

标签 统计

.net ×1

c# ×1