<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Run Windows Service as a console program</title>
	<atom:link href="http://einaregilsson.com/run-windows-service-as-a-console-program/feed/" rel="self" type="application/rss+xml" />
	<link>http://einaregilsson.com/run-windows-service-as-a-console-program/</link>
	<description>A site for my programming pet projects</description>
	<lastBuildDate>Mon, 23 Jan 2012 07:51:01 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: PiBa</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-75089</link>
		<dc:creator>PiBa</dc:creator>
		<pubDate>Sun, 31 Jul 2011 19:51:22 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-75089</guid>
		<description>@Darren, if i where you i would go with the SrvAny.exe which is available in the Windows NT Resource Kit. It can create a service of almost any program.. Not that it makes much sense to run Notepad.exe as a service. Who will ever type in it..?? and save the document.. :) :)

Though this is going a bit of the original topic which intents it the other way around, running a service as a consoleprogram. (for debugging)</description>
		<content:encoded><![CDATA[<p>@Darren, if i where you i would go with the SrvAny.exe which is available in the Windows NT Resource Kit. It can create a service of almost any program.. Not that it makes much sense to run Notepad.exe as a service. Who will ever type in it..?? and save the document.. <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Though this is going a bit of the original topic which intents it the other way around, running a service as a consoleprogram. (for debugging)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Darren</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-74792</link>
		<dc:creator>Darren</dc:creator>
		<pubDate>Mon, 13 Jun 2011 21:23:59 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-74792</guid>
		<description>@Luis: Your RunAsService.com sounds like an interesting idea, but raises a number of red flags: 
How do I know this isn&#039;t a trojan of some sort? Is the source code available? What kind of license is this provided under? 

Too many unknowns to make sense to even try it...</description>
		<content:encoded><![CDATA[<p>@Luis: Your RunAsService.com sounds like an interesting idea, but raises a number of red flags:<br />
How do I know this isn&#8217;t a trojan of some sort? Is the source code available? What kind of license is this provided under? </p>
<p>Too many unknowns to make sense to even try it&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luis</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-74418</link>
		<dc:creator>Luis</dc:creator>
		<pubDate>Sun, 01 May 2011 15:45:47 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-74418</guid>
		<description>If you just want to get your existing console application to run as a service without having to rewrite the code may I suggest a command line application that I wrote that can be found here: http://RunAsService.com</description>
		<content:encoded><![CDATA[<p>If you just want to get your existing console application to run as a service without having to rewrite the code may I suggest a command line application that I wrote that can be found here: <a href="http://RunAsService.com" rel="nofollow">http://RunAsService.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GaftAstot</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-73987</link>
		<dc:creator>GaftAstot</dc:creator>
		<pubDate>Sat, 29 Jan 2011 07:05:10 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-73987</guid>
		<description>87490.....84953</description>
		<content:encoded><![CDATA[<p>87490&#8230;..84953</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Some Doofus</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-71369</link>
		<dc:creator>Some Doofus</dc:creator>
		<pubDate>Thu, 12 Aug 2010 14:59:19 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-71369</guid>
		<description>Combined the info here into this helper class.
Just change proj output to console, and replace Run call.
&lt;code&gt;
//ServiceBase.Run(ServicesToRun);            WinServiceDebugging.NetServiceDebug.RunServices(ServicesToRun, args);

//----------NetServiceDebug.cs-------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Reflection;

namespace WinServiceDebugging
{
    // Adapted from author and commenters here&gt;
    // http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/
    /* &quot;You’ll have to right click on your project in Visual Studio, 
     * choose Properties and select the Output type as ‘Console application’ for this to work.&quot;
    */

    class NetServiceDebug
    {
        /// 
        /// Either runs service as console or windows service depending on Environment.UserInteractive
        /// 
        /// List of services to launch
        /// Command line parameters to pass along
        public static void RunServices(ServiceBase[] ServicesToRun, string[] args)
        {
            // Other alternatives to Environment.UserInteractive is to
            // 1. Use DEBUG conditional
            // 2. Use command line argument
            //#if (DEBUG)
            if (Environment.UserInteractive)
            {
                CallServiceMethod(ServicesToRun, &quot;OnStart&quot;, new object[] { args });
                WaitPrompt(ServicesToRun);
                CallServiceMethod(ServicesToRun, &quot;OnStop&quot;, null);
            }
            //#else
            else
            {
                ServiceBase.Run(ServicesToRun);
            }
            //#endif
        }

        private static void CallServiceMethod(ServiceBase[] ServicesToRun, string MethodName, object[] args)
        {
            Type type = typeof(ServiceBase);
            BindingFlags flags = BindingFlags.Instance &#124; BindingFlags.NonPublic;
            MethodInfo method = type.GetMethod(MethodName, flags);

            foreach (ServiceBase service in ServicesToRun)
            {
                method.Invoke(service, args);
            }
        }

        private static void WaitPrompt(ServiceBase[] ServicesToRun)
        {
            ConsoleKeyInfo Keypress = new ConsoleKeyInfo();
            do
            {
                Console.WriteLine(&quot;--- Press key ---&quot;);
                Console.WriteLine(&quot; p to pause,&quot;);
                Console.WriteLine(&quot; r to resume,&quot;);
                Console.WriteLine(&quot; s to stop&quot;);
                Console.WriteLine(&quot;------&quot;);
                Keypress = Console.ReadKey(true);
                switch (Keypress.Key)
                {
                    case ConsoleKey.P:
                        CallServiceMethod(ServicesToRun, &quot;OnPause&quot;, null);
                        Console.WriteLine(&quot;User&gt; PAUSE&quot;);
                        break;
                    case ConsoleKey.R:
                        CallServiceMethod(ServicesToRun, &quot;OnContinue&quot;, null);
                        Console.WriteLine(&quot;User&gt; RESUME&quot;);
                        break;
                    case ConsoleKey.S:
                        // Stop is called after the prompt, don&#039;t do it here
                        Console.WriteLine(&quot;User&gt; STOP&quot;);
                        break;
                }
            } while (Keypress.Key != ConsoleKey.S);
        }

    } // END CLASS: NetServiceDebug

} // END NAMESPACE
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Combined the info here into this helper class.<br />
Just change proj output to console, and replace Run call.<br />
<code><br />
//ServiceBase.Run(ServicesToRun);            WinServiceDebugging.NetServiceDebug.RunServices(ServicesToRun, args);</p>
<p>//----------NetServiceDebug.cs-------------<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.ServiceProcess;<br />
using System.Text;<br />
using System.Reflection;</p>
<p>namespace WinServiceDebugging<br />
{<br />
    // Adapted from author and commenters here&gt;<br />
    // <a href="http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/" rel="nofollow">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/</a><br />
    /* "You’ll have to right click on your project in Visual Studio,<br />
     * choose Properties and select the Output type as ‘Console application’ for this to work."<br />
    */</p>
<p>    class NetServiceDebug<br />
    {<br />
        ///<br />
        /// Either runs service as console or windows service depending on Environment.UserInteractive<br />
        ///<br />
        /// List of services to launch<br />
        /// Command line parameters to pass along<br />
        public static void RunServices(ServiceBase[] ServicesToRun, string[] args)<br />
        {<br />
            // Other alternatives to Environment.UserInteractive is to<br />
            // 1. Use DEBUG conditional<br />
            // 2. Use command line argument<br />
            //#if (DEBUG)<br />
            if (Environment.UserInteractive)<br />
            {<br />
                CallServiceMethod(ServicesToRun, "OnStart", new object[] { args });<br />
                WaitPrompt(ServicesToRun);<br />
                CallServiceMethod(ServicesToRun, "OnStop", null);<br />
            }<br />
            //#else<br />
            else<br />
            {<br />
                ServiceBase.Run(ServicesToRun);<br />
            }<br />
            //#endif<br />
        }</p>
<p>        private static void CallServiceMethod(ServiceBase[] ServicesToRun, string MethodName, object[] args)<br />
        {<br />
            Type type = typeof(ServiceBase);<br />
            BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;<br />
            MethodInfo method = type.GetMethod(MethodName, flags);</p>
<p>            foreach (ServiceBase service in ServicesToRun)<br />
            {<br />
                method.Invoke(service, args);<br />
            }<br />
        }</p>
<p>        private static void WaitPrompt(ServiceBase[] ServicesToRun)<br />
        {<br />
            ConsoleKeyInfo Keypress = new ConsoleKeyInfo();<br />
            do<br />
            {<br />
                Console.WriteLine("--- Press key ---");<br />
                Console.WriteLine(" p to pause,");<br />
                Console.WriteLine(" r to resume,");<br />
                Console.WriteLine(" s to stop");<br />
                Console.WriteLine("------");<br />
                Keypress = Console.ReadKey(true);<br />
                switch (Keypress.Key)<br />
                {<br />
                    case ConsoleKey.P:<br />
                        CallServiceMethod(ServicesToRun, "OnPause", null);<br />
                        Console.WriteLine("User&gt; PAUSE");<br />
                        break;<br />
                    case ConsoleKey.R:<br />
                        CallServiceMethod(ServicesToRun, "OnContinue", null);<br />
                        Console.WriteLine("User&gt; RESUME");<br />
                        break;<br />
                    case ConsoleKey.S:<br />
                        // Stop is called after the prompt, don't do it here<br />
                        Console.WriteLine("User&gt; STOP");<br />
                        break;<br />
                }<br />
            } while (Keypress.Key != ConsoleKey.S);<br />
        }</p>
<p>    } // END CLASS: NetServiceDebug</p>
<p>} // END NAMESPACE<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-71119</link>
		<dc:creator>Joel</dc:creator>
		<pubDate>Thu, 17 Jun 2010 05:30:27 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-71119</guid>
		<description>This is awesome!  Thanks for posting this!</description>
		<content:encoded><![CDATA[<p>This is awesome!  Thanks for posting this!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DaRage</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-67843</link>
		<dc:creator>DaRage</dc:creator>
		<pubDate>Thu, 28 May 2009 09:50:43 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-67843</guid>
		<description>I would never run multiple services in one process. This is against the service boundary definition and it&#039;s a hard and fast rule. because if once service crashes it will kill the others too. I&#039;ve seen it happen.

Too bad so much effort was spent to support multiple services in a process but I would stick with the simple initial version. simplicity is truth.</description>
		<content:encoded><![CDATA[<p>I would never run multiple services in one process. This is against the service boundary definition and it&#8217;s a hard and fast rule. because if once service crashes it will kill the others too. I&#8217;ve seen it happen.</p>
<p>Too bad so much effort was spent to support multiple services in a process but I would stick with the simple initial version. simplicity is truth.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kiran</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-67336</link>
		<dc:creator>kiran</dc:creator>
		<pubDate>Tue, 07 Apr 2009 18:29:06 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-67336</guid>
		<description>excellent</description>
		<content:encoded><![CDATA[<p>excellent</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kurt Harriger</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-67065</link>
		<dc:creator>Kurt Harriger</dc:creator>
		<pubDate>Fri, 06 Mar 2009 02:31:32 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-67065</guid>
		<description>I&#039;ve used this approach for awhile.  But I have discovered one small problem with using UserInteractive flag.  If you run the process via windows task scheduler UserInteractive will be false and it will try to start as a service and fail. Somehting to keep in mind anyway.  If you find a solution to it I&#039;d love to know what it is, but at the moment the best approach I&#039;ve found is to use a commandline argument to override the UserInteractive flag.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve used this approach for awhile.  But I have discovered one small problem with using UserInteractive flag.  If you run the process via windows task scheduler UserInteractive will be false and it will try to start as a service and fail. Somehting to keep in mind anyway.  If you find a solution to it I&#8217;d love to know what it is, but at the moment the best approach I&#8217;ve found is to use a commandline argument to override the UserInteractive flag.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ThomasK</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/comment-page-1/#comment-61165</link>
		<dc:creator>ThomasK</dc:creator>
		<pubDate>Sat, 10 Jan 2009 05:14:43 +0000</pubDate>
		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/#comment-61165</guid>
		<description>I do not see this behaviour here. My application is a console app, but no matter how i start it (from cosole or SCM) i don&#039;t see a new console window appear.</description>
		<content:encoded><![CDATA[<p>I do not see this behaviour here. My application is a console app, but no matter how i start it (from cosole or SCM) i don&#8217;t see a new console window appear.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

