GC.KeepAlive versus using

后端 未结 3 882
日久生厌
日久生厌 2021-01-01 19:06

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

static void Main()                  // args are O         


        
相关标签:
3条回答
  • Using using seems to be a better fit in this case than using GC.KeepAlive. You not only want to keep the Mutex alive as long as the application is running, you also want it to go away as soon as you exit the main loop.

    If you just leave the Mutex hanging without disposing it, there may be some time before it's finalized, depending on how much cleanup work there is to do when the application is shutting down.

    0 讨论(0)
  • 2021-01-01 19:31

    I think the reason KeepAlive is used with the named Mutex is to make sure it IS NOT garbage collected early. The C# using / Dispose pattern is not for protected against that. When an object reference is no longer used within a scope the runtime may collect it before the end of the scope. It's an optimiztion.

    0 讨论(0)
  • 2021-01-01 19:53

    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).

    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.

    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.

    0 讨论(0)
提交回复
热议问题