Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general yes this will work. However the devil is in the details.</p> <p>Firstly you want to close the mutex in a <code>finally</code> block. Otherwise your process could abruptly terminate and leave it in a signaled state, like an exception. That would make it so that future process instances would not be able to start up. </p> <p>Unfortunately though, even with a <code>finally</code> block you must deal with the potential that a process will be terminated without freeing up the mutex. This can happen for instance if a user kills the process through TaskManager. There is a race condition in your code that would allow for a second process to get an <code>AbandonedMutexException</code> in the <code>WaitOne</code> call. You'll need a recovery strategy for this. </p> <p>I encourage you to read up on <a href="http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx" rel="noreferrer">the details of the Mutex class</a>. Using it is not always simple.</p> <hr> <p>Expanding upon the race condition possibility:</p> <p>The following sequence of events can occur which would cause a second instance of the application to throw:</p> <ol> <li>Normal process startup.</li> <li>Second process starts up and aquires a handle to the mutex but is switched out before the <code>WaitOne</code> call.</li> <li>Process #1 is abruptly terminated. The mutex is not destroyed because process #2 has a handle. It is instead set to an abandoned state.</li> <li>The second process starts running again and gets an <code>AbanonedMutexException</code>.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload