This project has moved and is read-only. For the latest updates, please go here.

Excellent, but throwing an exception for the first instance is not clean

Feb 24, 2012 at 1:28 AM

I really do like the idea, but don't like throwing exceptions in a valid path of code.

If you add a bit of code and use the Mutex, you can determine if you are the first instance without the exception. Then the code is clean and the load time of the first instance is faster.

just add 'static Mutex m;' to the class and update Make to:

            bool isnew;
            m = new Mutex(true, appName, out isnew);
            if (!isnew)
                using (var eventWaitHandle = EventWaitHandle.OpenExisting(eventWaitHandleName))
                    // It informs first instance about other startup attempting.

                // Let's terminate this posterior startup.
                // For that exit no interceptions.
	        using (var eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventWaitHandleName))
	            ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, OtherInstanceAttemptedToStart, null, Timeout.Infinite,

What do you think?