0

When I run my Windows Service with --console from a command prompt the console.txt file gets created so I know the ConsoleMode() method is being fired but nothing gets written to the console window. I know that Windows Services can't write to the console but shouldn't it just be running as a normal Console app since I bypass ServiceBase.Run?

using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ShowCheckerService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(params string[] args)
        {



    #if DEBUG
            Service1 myService = new Service1();
            myService.OnDebug();
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

    #else
            if (Environment.UserInteractive)
            {
                string parameter = string.Concat(args);
                switch (parameter)
                {
                    case "--install":
                        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    case "--uninstall":
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                        break;
                    case "--console":
                        ConsoleMode();
                        break;
                }
            }

            else
            {

                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new Service1()
                };
                ServiceBase.Run(ServicesToRun);
            }
    #endif

        }


        private static void ConsoleMode()
        {
            System.IO.File.Create(@"C:\ProgramData\ShowChecker\console.txt");
            Console.WriteLine("asdf");            
        }


    }
}
Jrow
  • 1,026
  • 2
  • 12
  • 31

2 Answers2

1

Had to change this in the properties:

enter image description here

Jrow
  • 1,026
  • 2
  • 12
  • 31
0

A Windows service can actually Write A Console buffer, in fact i had once a console.read() and my service hung on that line look here https://stackoverflow.com/a/8793030/5601607

Community
  • 1
  • 1