<?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; .NET</title>
	<atom:link href="http://andrewbeaton.net/faq/tag/net/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, 13 Jul 2011 17:03:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<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; // [...]]]></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: #6666cc; font-weight: bold;">bool</span> alone <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
Mutex m <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mutex<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #666666;">&quot;&lt;name&gt;&quot;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> alone<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>alone<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Another instance is already running.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Running...<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#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: #008000;">.</span><span style="color: #0000FF;">KeepAlive</span><span style="color: #008000;">&#40;</span>m<span style="color: #008000;">&#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>0</slash:comments>
		</item>
		<item>
		<title>Restart a Windows service using C#</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/restart-a-windows-service-using-csharp/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/restart-a-windows-service-using-csharp/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:38:05 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ServiceController]]></category>
		<category><![CDATA[ServiceControllerStatus]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[TimeSpan]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=11</guid>
		<description><![CDATA[This example shows how to start, stop and restart a windows service programmatically in C#. Start service The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs. public static void StartService&#40;string serviceName, int timeoutMilliseconds&#41; &#123; ServiceController service = new ServiceController&#40;serviceName&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>This example shows how to start, stop and restart a windows service programmatically in C#. </p>
<p><strong>Start service</strong></p>
<p>The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> StartService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> serviceName, <span style="color: #6666cc; font-weight: bold;">int</span> timeoutMilliseconds<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #008000;">&#40;</span>serviceName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMilliseconds</span><span style="color: #008000;">&#40;</span>timeoutMilliseconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">WaitForStatus</span><span style="color: #008000;">&#40;</span>ServiceControllerStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">Running</span>, timeout<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Stop service</strong></p>
<p>The following method tries to stop the specified service and it waits until the service is stopped or a timeout occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> StopService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> serviceName, <span style="color: #6666cc; font-weight: bold;">int</span> timeoutMilliseconds<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #008000;">&#40;</span>serviceName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMilliseconds</span><span style="color: #008000;">&#40;</span>timeoutMilliseconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">WaitForStatus</span><span style="color: #008000;">&#40;</span>ServiceControllerStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">Stopped</span>, timeout<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Restart service</strong></p>
<p>This method combinates both previous methods. It tries to stop the service (and waits until it&#8217;s stopped) then it begins to start the service (and waits until the service is running). The specified timeout is used for both operations together.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RestartService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> serviceName, <span style="color: #6666cc; font-weight: bold;">int</span> timeoutMilliseconds<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #008000;">&#40;</span>serviceName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> millisec1 <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">TickCount</span><span style="color: #008000;">;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMilliseconds</span><span style="color: #008000;">&#40;</span>timeoutMilliseconds<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">WaitForStatus</span><span style="color: #008000;">&#40;</span>ServiceControllerStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">Stopped</span>, timeout<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// count the rest of the timeout</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> millisec2 <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">TickCount</span><span style="color: #008000;">;</span>
        timeout <span style="color: #008000;">=</span> TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMilliseconds</span><span style="color: #008000;">&#40;</span>timeoutMilliseconds <span style="color: #008000;">-</span> <span style="color: #008000;">&#40;</span>millisec2<span style="color: #008000;">-</span>millisec1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        service<span style="color: #008000;">.</span><span style="color: #0000FF;">WaitForStatus</span><span style="color: #008000;">&#40;</span>ServiceControllerStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">Running</span>, timeout<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">catch</span>
   <span style="color: #008000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// ...</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/restart-a-windows-service-using-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Read source of a web page in C#</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/read-source-of-a-web-page-in-csharp/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/read-source-of-a-web-page-in-csharp/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:31:03 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[WebRequest]]></category>
		<category><![CDATA[WebResponse]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=3</guid>
		<description><![CDATA[This example shows how to read the source of a web page in C#. // used to build entire input StringBuilder sb = new StringBuilder&#40;&#41;; &#160; // used on each read operation byte&#91;&#93; buf = new byte&#91;8192&#93;; &#160; // prepare the web page we will be asking for WebRequest request  = WebRequest.Create&#40;&#34;http://andrewbeaton.com&#34;&#41;; &#160; // execute [...]]]></description>
			<content:encoded><![CDATA[<p>This example shows how to read the source of a web page in C#.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// used to build entire input</span>
StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// used on each read operation</span>
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buf <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">8192</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// prepare the web page we will be asking for</span>
WebRequest request  <span style="color: #008000;">=</span> WebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://andrewbeaton.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// execute the request</span>
WebResponse response <span style="color: #008000;">=</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// we will read data via the response stream</span>
Stream resStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">string</span> tempString <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">int</span> count <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">do</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// fill the buffer with data</span>
    count <span style="color: #008000;">=</span> resStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>buf, <span style="color: #FF0000;">0</span>, buf<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// make sure we read some data</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>count <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// translate from bytes to ASCII text</span>
        tempString <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>buf, <span style="color: #FF0000;">0</span>, count<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// continue building the string</span>
       sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>tempString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>count <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/read-source-of-a-web-page-in-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

