为什么要使用try {}终于{}用空try块? [英] Why use try {} finally {} with an empty try block?

查看:559
本文介绍了为什么要使用try {}终于{}用空try块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在 System.Threading.TimerBase.Dipose()该方法具有尝试{}终于{} 块,但尝试{} 是空的。

}使用任何值尝试{最后{} 有一个空的尝试?

<一个href="http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.Threading&type=TimerBase">http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.Threading&type=TimerBase

  [ReliabilityContract(Consistency.WillNotCorruptState,Cer.MayFail)
内部布尔的Dispose(WaitHandle的notifyObject)
{
    BOOL状态= FALSE;
    布尔bLockTaken = FALSE;
    RuntimeHelpers prepareConstrainedRegions()。
    尝试 {
    }
    最后 {
        做 {
            如果(Interlocked.CompareExchange(参考m_lock,1,0)== 0){
                bLockTaken =真;
                尝试 {
                    状态= DeleteTimerNative(notifyObject.SafeWaitHandle);
                }
                最后 {
                    m_lock = 0;
                }
            }
            Thread.SpinWait(1);
            //屈服于处理器
        }
        而(bLockTaken!);
        GC.Sup pressFinalize(本);
    }

    返回状态;
}
 

解决方案

从<一个href="http://web.archive.org/web/20130523155042/http://blog.somecreativity.com/2008/04/10/the-empty-try-block-mystery/">http://blog.somecreativity.com/2008/04/10/the-empty-try-block-mystery/:

  

这对一个方法后卫   Thread.Abort的呼叫中断   处理。的MSDN页面   Thread.Abort的说:未执行   finally块之前执行   线程被中止。因此,为了   保证您的处理   完成即使你的线程   中止在中间由某人   在你的线程中调用中止,您可以   将所有的code在最后   块(另一种方法是写   code。在catch块以确定   你在哪里尝试之前是   通过中止中断,坚持从   还有,如果你愿意的话)。

I noticed in System.Threading.TimerBase.Dipose() the method has a try{} finally{} block but the try{} is empty.

Is there any value in using try{} finally{} with an empty try?

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;
}

解决方案

From http://blog.somecreativity.com/2008/04/10/the-empty-try-block-mystery/:

This methodology guards against a Thread.Abort call interrupting the processing. The MSDN page of Thread.Abort says that "Unexecuted finally blocks are executed before the thread is aborted". So in order to guarantee that your processing finishes even if your thread is aborted in the middle by someone calling Abort on your thread, you can place all your code in the finally block (the alternative is to write code in the "catch" block to determine where you were before "try" was interrupted by Abort and proceed from there if you want to).

这篇关于为什么要使用try {}终于{}用空try块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆