<?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; TimeSpan</title>
	<atom:link href="http://andrewbeaton.net/faq/tag/timespan/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>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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> StartService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serviceName, <span style="color: #FF0000;">int</span> timeoutMilliseconds<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #000000;">&#40;</span>serviceName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Running</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">catch</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> StopService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serviceName, <span style="color: #FF0000;">int</span> timeoutMilliseconds<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #000000;">&#40;</span>serviceName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Stopped</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">catch</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RestartService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serviceName, <span style="color: #FF0000;">int</span> timeoutMilliseconds<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #000000;">&#40;</span>serviceName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> millisec1 <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">TickCount</span><span style="color: #008000;">;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Stopped</span>, timeout<span style="color: #000000;">&#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: #FF0000;">int</span> millisec2 <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">TickCount</span><span style="color: #008000;">;</span>
        timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds <span style="color: #008000;">-</span> <span style="color: #000000;">&#40;</span>millisec2<span style="color: #008000;">-</span>millisec1<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Running</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span>
   <span style="color: #000000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// ...</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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>2</slash:comments>
		</item>
	</channel>
</rss>
