Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 143637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:07:37+00:00 2026-05-11T08:07:37+00:00

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

  • 0

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

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.

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:07:37+00:00Added an answer on May 11, 2026 at 8:07 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this article on Code Project that talks about using an abstract
I just read an article in the BlackBerry docs about writing efficient code in
In his sitepoint article about javascript inheritance , Harry Fuecks explains a way of
Hi i am following this tutorial http://www.vogella.de/articles/AndroidGestures/article.html i want to create an application in
Just read this interesting article by Omar on his blog Linq to SQL solve
I have a colleague who insists that his code doesn't need comments, it's "self
I saw this article pop-up in my MSDN RSS feed, and after reading through
I was reading Stoyan Stefanov's excellent article about JS domain sharding for image files
I was reading the article from Steve Yegge about singletons. In it he mentions
I was reading an article here: http://javascriptweblog.wordpress.com/2010/03/16/five-ways-to-create-objects/ It tells about five ways of creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.