<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrew Beaton :: FAQ &#187; Mutex</title>
	<atom:link href="http://andrewbeaton.net/faq/tag/mutex/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrewbeaton.net/faq</link>
	<description>Tips from the world of a software developer</description>
	<lastBuildDate>Wed, 10 Mar 2010 21:03:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Run only one instance of a program in C#</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/run-only-one-instance-of-a-program-in-csharp/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/run-only-one-instance-of-a-program-in-csharp/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:45:22 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Mutex]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=15</guid>
		<description><![CDATA[This example shows you how to ensure that only one instance of an application is running.

// Ensure only one instance of this application is running.
bool alone = false;
Mutex m = new Mutex&#40;true, &#34;&#60;name&#62;&#34;, out alone&#41;;
&#160;
if &#40;!alone&#41;
&#123;
    Console.WriteLine&#40;&#34;Another instance is already running.&#34;&#41;;
    Console.ReadKey&#40;&#41;;
    return;
&#125;
&#160;
Console.WriteLine&#40;&#34;Running...\n&#34;&#41;;
Console.ReadKey&#40;&#41;;
&#160;
// Needed to ensure [...]]]></description>
			<content:encoded><![CDATA[<p>This example shows you how to ensure that only one instance of an application is running.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Ensure only one instance of this application is running.</span>
<span style="color: #FF0000;">bool</span> alone <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
Mutex m <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mutex<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;&lt;name&gt;&quot;</span>, <span style="color: #0600FF;">out</span> alone<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>alone<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Another instance is already running.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Console.<span style="color: #0000FF;">ReadKey</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    return<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Running...<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadKey</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Needed to ensure only one instance is running.</span>
GC.<span style="color: #0000FF;">KeepAlive</span><span style="color: #000000;">&#40;</span>m<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/run-only-one-instance-of-a-program-in-csharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
