是C#的using语句中止安全的吗? [英] Is C#'s using statement abort-safe?

查看:200
本文介绍了是C#的using语句中止安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看完C#4.0果壳中的(O'Reilly出版),我认为这是愿意转换到C#程序员一个伟大的书,但它留给我的疑惑。我的问题是使用语句的定义。根据书(第138页),

I've just finished reading "C# 4.0 in a Nutshell" (O'Reilly) and I think it's a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138),

using (StreamReader reader = File.OpenText("file.txt")) {
    ...
}

是完全等效于:

StreamReader reader = File.OpenText("file.txt");
try {
    ...
} finally {
    if (reader != null)
        ((IDisposable)reader).Dispose();
}



然而,假设这是真的,并且该代码是在执行单独的线程。这个线程现在中止与 Thread.Abort的(),所以 ThreadAbortException 被抛出,并假设该线程是完全后初始化读取器和输入 try..finally 子句之前。 !这将意味着读者不设置

Suppose, however, that this is true and that this code is executed in a separate thread. This thread is now aborted with thread.Abort(), so a ThreadAbortException is thrown and suppose the thread is exactly after initializing the reader and before entering the try..finally clause. This would mean that the reader is not disposed!

一个可能的解决方案将是,以这种方式编码:

A possible solution would be to code this way:

StreamReader reader = null;
try {
    reader = File.OpenText("file.txt");
    ...
} finally {
    if (reader != null)
        ((IDisposable)reader).Dispose();
}

这是中止安全的。

对于我的问题现在:


  1. 是在使用<书权和作者/ code>语句不中止安全或者是他们错了,它的行为就像我的第二个解决方案?

  2. <使用li>如果 相当于第一变型(未中止安全的),为什么它在最后
    LI>
  3. 根据书(第856), ThreadAbortException 随处可在托管代码中抛出。但也许也有例外毕竟

  1. Are authors of the book right and the using statement is not abort-safe or are they wrong and it behaves like in my second solution?
  2. If using is equivalent to the first variant (not abort-safe), why does it check for null in finally?
  3. According to the book (p. 856), ThreadAbortException can be thrown anywhere in managed code. But maybe there are exceptions and the first variant is abort-safe after all?

修改的第一个变种是中止安全:我知道在使用 Thread.Abort的()不被认为是很好的做法。我的兴趣是纯粹的理论:请问在使用语句的行为究竟

I know that using thread.Abort() is not considered good practice. My interest is purely theoretical: how does the using statement behave exactly?

推荐答案

这本书的配套网站对中止线程这里更多信息。

The book's companion web site has more info on aborting threads here.

总之,第一次翻译是正确的(你可以通过观察IL告诉)。

In short, the first translation is correct (you can tell by looking at the IL).

回答你的第二个问题是,有可能是场景中的变量可以是合法空。例如,的getFoo()返回null在这里,在你不希望一个NullReferenceException中隐含的最终块抛出:

The answer to your second question is that there may be scenarios where the variable can be legitimately null. For instance, GetFoo() may return null here, in which you wouldn't want a NullReferenceException thrown in the implicit finally block:

using (var x = GetFoo())
{
   ...
}

要回答你的第三个问题,作出中止安全(如果你调用框架代码)的唯一途径就是后来推倒的AppDomain。这实际上是在很多情况下,一个可行的解决方案(这是LINQPad究竟是干什么的,只要你取消正在运行的查询)。

To answer your third question, the only way to make Abort safe (if you're calling Framework code) is to tear down the AppDomain afterward. This is actually a practical solution in many cases (it's exactly what LINQPad does whenever you cancel a running query).

这篇关于是C#的using语句中止安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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