对于是否关闭处置警告此ReSharper的修复有什么意义? [英] Does this Resharper fix for disposed closure warning make any sense?

查看:221
本文介绍了对于是否关闭处置警告此ReSharper的修复有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的得到从静态code分析去掉了一些警告。 在一个特定的情况下,没有处分权被上做了的ManualResetEvent

I'm working on getting rid of some warnings from a static code analysis. In one specific case there was no disposing being done on a ManualResetEvent.

在code有关执行 Func键主线程和阻塞调用线程一定毫秒数上。我知道这听起​​来像一个奇怪的事情,但它是这个问题的范围之内,所以多多包涵。

The code in question executes a Func on the main thread and blocks the calling thread for a certain number of milliseconds. I realize this sounds like a weird thing to do, but it's outside the scope of this question, so bear with me.

假如我加了使用语句,像这样:

Suppose I add a using statement like so:

object result = null;
using (var completedEvent = new ManualResetEvent(false))
{
    _dispatcher.BeginInvoke((Action)(() =>
        {
            result = someFunc;
            completedEvent.Set();  // Here be dragons!
        }));

    completedEvent.WaitOne(timeoutMilliseconds);
    return result;
}

现在,我意识到,这很可能会导致问题。我也正好使用 ReSharper的,并警告我的消息访问处置倒闭。

Now, I realize that this is likely to cause problems. I also happen to use Resharper and it warns me with the message "Access to disposed closure".

ReSharper的建议,通过改变问题的行来解决这个问题:

Resharper proposes to fix this by changing the offending line to:

if (completedEvent != null)
{ 
    completedEvent.Set();
}

现在,提出的解决方案我迷惑不解。在正常情况下,也就没有理由的变量将由使用语句设置为有一些实现细节在.NET关闭,将保证该变量为之后已经关闭了该变量被设置?

Now, the proposed solution puzzles me. Under normal circumstances, there would be no reason why a variable would be set to null by a using statement. Is there some implementation detail to closures in .NET that would guarantee the variable to be null after the variable that has been closed over is disposed?

作为奖励的问题,这将是一个很好的解决处理问题的的ManualResetEvent

As a bonus question, what would be a good solution to the problem of disposing the ManualResetEvent?

推荐答案

正在混淆了ReSharper的速战速决和环境行动。当ReSharper的建议解决某个问题,那么很有可能你会看到一个灯泡出现。你不会看到一个球在这里,因为没有快速解决这一警告。

You are mixing up ReSharper's "quick fix" and "context action". When ReSharper proposes to fix something, then most likely you'd see a bulb there. You don't see a bulb here, because there are no quick fixes to this warning.

但是,除了快速修复ReSharper的也有背景下的行为,它可以为你做一些日常任务(把它们像一个小重构)。当ReSharper的具有下光标code上下文的动作,它会告诉你一个选秀权。在这里,你看到所谓的上下文行动的检查,如果事情是不为空。它没有任何关系的警告,并没有约定处理变量之后会被设置为null。

But aside from quick fixes ReSharper also has "context actions", where it can do some routine tasks for you (think of them like a small refactorings). When ReSharper has a context action for a code under cursor, it would show you a pick. Here you see a context action called "Check if something is not null". It has no relation to a warning, and there is no convention that after disposing a variable would be set to null.

此外,当您preSS的Alt-Enter键,你会看到一个删除线的出球给你即时pression的ReSharper的不提出任何快速解决此警告,但可以将其禁用有意见。事实上,这是使这个警告很容易消失的唯一途径。但我已经重写了这片code来代替。

Also, when you press Alt-Enter, you'd see a striked-out bulb to give you impression that ReSharper doesn't suggest any quick fixes for this warning, but it can disable it with comments. In fact, that's the only way to make this warning go away easily. But I'd rewrite this piece of code instead.

这篇关于对于是否关闭处置警告此ReSharper的修复有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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