GC.KeepAlive与使用 [英] GC.KeepAlive versus using

查看:167
本文介绍了GC.KeepAlive与使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在他的文章有关防止多个实例申请后,迈克尔·温顿提出以下代码:<当然
{
布尔OK / p>

In his article about preventing multiple instances of an application, Michael Covington presents this code:

static void Main()                  // args are OK here, of course
{
    bool ok;
    m = new System.Threading.Mutex(true, "YourNameHere", out ok);

    if (! ok)
    {
        MessageBox.Show("Another instance is already running.");
        return;
    }

    Application.Run(new Form1());   // or whatever was there

    GC.KeepAlive(m);                // important!
}

他解释说,GC.KeepAlive(M)是为了防止垃圾。收藏家从早期收集互斥,因为有它不需要额外的引用

He explains that the GC.KeepAlive(m) is required to prevent the garbage collector from collecting the mutex early, since there are no additional references to it.

我的问题:将包裹互斥的使用做同样的事情?也就是说,将下面还阻止GC下,我从拉地毯?

My question: will wrapping the mutex in a using do the same thing? That is, will the following also prevent the GC from pulling the rug out from under me?

static void Main()                  // args are OK here, of course
{
    bool ok;
    using (var m = new System.Threading.Mutex(true, "YourNameHere", out ok))
    {
        if (! ok)
        {
            MessageBox.Show("Another instance is already running.");
            return;
        }

        Application.Run(new Form1());   // or whatever was there
    }
}



我的直觉反应是,在使用将工作,因为使用的(应该​​是)相当于:

My gut reaction is that the using will work, since using is (supposed to be) equivalent to:

Mutex m = new System.Threading.Mutex(true, "YourNameHere", out ok);
try
{
    // do stuff here
}
finally
{
    m.Close();
}

和我倒觉得m.Close()将有足够的信号的JIT编译器,还有另一种参考,从而避免过早垃圾收集。

And I would think that the m.Close() there would be enough to signal to the JIT compiler that there's another reference, thus preventing premature garbage collection.

推荐答案

结束语互斥在使用语句确实会阻止它被垃圾收集,而是将的处置(它调用的Dispose ,不是关闭)在结束(而 GC.KeepAlive 不会,很明显)。

Wrapping the mutex in a using statement will indeed prevent it from being garbage collected, but will also dispose of it (it calls Dispose, not Close) at the end (whereas GC.KeepAlive won't, obviously).

如果该方法到底是真正的将是这个过程的结束,我不相信这是可能让你使用任何实际的差异 - 我更喜欢使用上的任何处理的一般原则语句,它实现的IDisposable

If the end of the method is genuinely going to be the end of the process, I don't believe it's likely to make any practical difference which you use - I prefer the using statement on the general principle of disposing of anything which implements IDisposable.

如果在退出过程中,我怀疑它的终结会照顾它的互斥锁尚未处理 - 只要其他终结别占着定稿线程超出其超时

If the mutex hasn't been disposed when the process exits, I suspect its finalizer will take care of it - so long as other finalizers don't hog the finalization thread beyond its timeout.

如果终结不照顾它,我不知道自己的Windows是否会发现,这个过程不可能拥有互斥更多,因为它(这个过程)不存在任何更多。我怀疑这会,但你必须详细检查Win32文档就知道肯定。

If the finalizer doesn't take care of it, I don't know whether Windows itself will notice that the process can't possibly own the mutex any more, given that it (the process) doesn't exist any more. I suspect it will, but you'd have to check detailed Win32 documentation to know for sure.

这篇关于GC.KeepAlive与使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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