15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

synchronization ❘ 523<br />

The classes Mutex, EventWaitH<strong>and</strong>le, <strong>and</strong> Semaphore are derived from the base class WaitH<strong>and</strong>le, so you<br />

can use all of these with waits.<br />

mutex<br />

Mutex (mutual exclusion) is one of the classes of the .<strong>NET</strong> Framework that offers synchronization across<br />

multiple processes. It is very similar to the Monitor class in that there is just one owner. Just one thread can<br />

get a lock on the mutex <strong>and</strong> access the synchronized code regions that are secured by the mutex.<br />

With the constructor of the Mutex class, you can define if the mutex should initially be owned by the calling<br />

thread, define a name for the mutex, <strong>and</strong> get the information if the mutex already exists. In the sample<br />

code, the third parameter is defined as an out parameter to receive a Boolean value if the mutex was newly<br />

created. If the value returned is false, the mutex was already defined. The mutex might be defined in<br />

a different process, because a mutex with a name is known to the operating system <strong>and</strong> is shared among<br />

different processes. If there is no name assigned to the mutex, the mutex is unnamed <strong>and</strong> not shared<br />

among different processes.<br />

bool createdNew;<br />

Mutex mutex = new Mutex(false, "ProCSharpMutex", out createdNew);<br />

To open an existing mutex, you can also use the method Mutex.OpenExisting(), which doesn’t require the<br />

same .<strong>NET</strong> privileges as creating the mutex with the constructor.<br />

Because the Mutex class derives from the base class WaitH<strong>and</strong>le, you can do a WaitOne() to acquire<br />

the mutex lock <strong>and</strong> be the owner of the mutex during that time. The mutex is released by invoking the<br />

ReleaseMutex() method.<br />

if (mutex.WaitOne())<br />

{<br />

try<br />

{<br />

// synchronized region<br />

}<br />

finally<br />

{<br />

mutex.ReleaseMutex();<br />

}<br />

}<br />

else<br />

{<br />

// some problem happened while waiting<br />

}<br />

Because a named mutex is known system-wide, you can use it to keep an application from being started<br />

twice. In the following Windows Forms application, the constructor of the Mutex object is invoked.<br />

Then, it is verified if the mutex with the name SingletonWinAppMutex exists already. If it does, the<br />

application exits.<br />

static class Program<br />

{<br />

[STAThread]<br />

static void Main()<br />

{<br />

bool createdNew;<br />

Mutex mutex = new Mutex(false, "SingletonWinAppMutex",<br />

out createdNew);<br />

if (!createdNew)<br />

{<br />

MessageBox.Show("You can only start one instance " +<br />

"of the application");<br />

Application.Exit();<br />

return;<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!