Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows or Console application target for Windows Service?

Windows or Console application target for Windows Service?

Scheduled Pinned Locked Moved C#
csharpvisual-studiowinformscomquestion
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeSchmoe007
    wrote on last edited by
    #1

    When Windows Service application is created by VS 2008 via "Windows Service" template it is created as Windows application ( /target:winexe) For convenience of debugging I modified it so that it can be started both from the command line and as a service using this approach:

        static void Main(string\[\] args)
        {
            ServiceBase\[\] ServicesToRun;
            TestService srv = new TestService();
            ServicesToRun = new ServiceBase\[\] 
    		{ 
    			srv
    		};
            if (Environment.UserInteractive)
            {
                char KeyChar;
                srv.ManualStart(args);
                Console.WriteLine("Service started in console mode, press Space key to stop");
                Console.WriteLine("Press any letter or digit to send control command (ASCII code +128)");
                while (true)
                {
                    KeyChar = Console.ReadKey().KeyChar;
                    Console.WriteLine("");
    
                    if (KeyChar == ' ')
                        break;
                    else
                    {
                        srv.ManualCustomCommand(KeyChar + 128);
                    }
    
                }
    
                srv.ManualStop();
    
            }
            else
                ServiceBase.Run(ServicesToRun);
        }
    

    In order for this to work as expected I had to change output type from "Windows Application" to "Console Application" ( /target:exe). Everything currently works the way I expect. Is this acceptable for Windows Service to be compiled as Console application? Are there any implications? Should I have kept it as "Windows Application" and attached console to it using approach described here: http://www.csharp411.com/console-output-from-winforms-application/ ?

    D P 2 Replies Last reply
    0
    • J JoeSchmoe007

      When Windows Service application is created by VS 2008 via "Windows Service" template it is created as Windows application ( /target:winexe) For convenience of debugging I modified it so that it can be started both from the command line and as a service using this approach:

          static void Main(string\[\] args)
          {
              ServiceBase\[\] ServicesToRun;
              TestService srv = new TestService();
              ServicesToRun = new ServiceBase\[\] 
      		{ 
      			srv
      		};
              if (Environment.UserInteractive)
              {
                  char KeyChar;
                  srv.ManualStart(args);
                  Console.WriteLine("Service started in console mode, press Space key to stop");
                  Console.WriteLine("Press any letter or digit to send control command (ASCII code +128)");
                  while (true)
                  {
                      KeyChar = Console.ReadKey().KeyChar;
                      Console.WriteLine("");
      
                      if (KeyChar == ' ')
                          break;
                      else
                      {
                          srv.ManualCustomCommand(KeyChar + 128);
                      }
      
                  }
      
                  srv.ManualStop();
      
              }
              else
                  ServiceBase.Run(ServicesToRun);
          }
      

      In order for this to work as expected I had to change output type from "Windows Application" to "Console Application" ( /target:exe). Everything currently works the way I expect. Is this acceptable for Windows Service to be compiled as Console application? Are there any implications? Should I have kept it as "Windows Application" and attached console to it using approach described here: http://www.csharp411.com/console-output-from-winforms-application/ ?

      D Offline
      D Offline
      darkelv
      wrote on last edited by
      #2

      I would arrange all the stuff that you want to run as the service into another class project. Then you can have the Windows Service project reference to this project for actual usage, and create console app project or windows app project that reference the class project for debugging.

      1 Reply Last reply
      0
      • J JoeSchmoe007

        When Windows Service application is created by VS 2008 via "Windows Service" template it is created as Windows application ( /target:winexe) For convenience of debugging I modified it so that it can be started both from the command line and as a service using this approach:

            static void Main(string\[\] args)
            {
                ServiceBase\[\] ServicesToRun;
                TestService srv = new TestService();
                ServicesToRun = new ServiceBase\[\] 
        		{ 
        			srv
        		};
                if (Environment.UserInteractive)
                {
                    char KeyChar;
                    srv.ManualStart(args);
                    Console.WriteLine("Service started in console mode, press Space key to stop");
                    Console.WriteLine("Press any letter or digit to send control command (ASCII code +128)");
                    while (true)
                    {
                        KeyChar = Console.ReadKey().KeyChar;
                        Console.WriteLine("");
        
                        if (KeyChar == ' ')
                            break;
                        else
                        {
                            srv.ManualCustomCommand(KeyChar + 128);
                        }
        
                    }
        
                    srv.ManualStop();
        
                }
                else
                    ServiceBase.Run(ServicesToRun);
            }
        

        In order for this to work as expected I had to change output type from "Windows Application" to "Console Application" ( /target:exe). Everything currently works the way I expect. Is this acceptable for Windows Service to be compiled as Console application? Are there any implications? Should I have kept it as "Windows Application" and attached console to it using approach described here: http://www.csharp411.com/console-output-from-winforms-application/ ?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Windows Services have no GUI so Winexe makes no sense. I always compile service programs as console, but more to the point, I compile the actual service code in a DLL which the program calls. I do pretty much what you show there and control my services from the command line. Edit: When I write an application that can be used in console or WinForms mode, I compile it as console.

        modified on Monday, July 26, 2010 1:21 PM

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups