1

To debug a windows service in visual studio I manually open a console window to leave the service running by calling console.ReadKey(true) rather than using a while(true)...

This has been working for years but recently I get an exception:
"Cannot read keys when either application does not have a console or when console input has been redirected from a file"

Projects Output Type is "Windows Application" which is the default for windows service. According to the accepted answer in Running a Windows Service in Console mode? I tried to set it to "Concole Application". The console is shown (without my ShowConsoleWindow call) but I don't know which side effects might occur if a real windows service suddenly has a console in RELEASE so I prefer to leave it as "Windows Application".

This is the startup routine

static void Main(string[] args)
{
    #if (!DEBUG)
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new HostService(args) };
        ServiceBase.Run(ServicesToRun);
    #else
        InteropWindows.ShowConsoleWindow();

        using (HostService srv = new HostService(args))
        {
            srv.MyRun(args);

            Console.WriteLine("Host Service is running. Press any key to stop...");
            Console.ReadKey(true);
            Console.WriteLine("Stopping service...");

            srv.MyStop();
        }
    #endif 
}

InteropWindows.ShowConsoleWindow():

public static void ShowConsoleWindow()
{
    var handle = NativeMethods.GetConsoleWindow();

    if (handle == IntPtr.Zero)
    {
        bool succcess = NativeMethods.AllocConsole();
        uint lastErr = NativeMethods.GetLastError();
    }
    else
        ShowWindow(handle, ShowWindowCommand.Show);
}

NativeMethods.AllocConsole() returns true, NativeMethods.GetLastError() is 0 but the new console window does no longer open (as I said it used to work for years).
Console.WriteLine() does not print anything, but does not throw either.
Console.ReadKey(true); in Main program then throws the exception shown above.

Following the exception's advise to use Console.Read() instead, just blocks the application infinitly so I could just use while(true);

Any ideas?

TomB
  • 641
  • 5
  • 17
  • What OS are you running? – cly Mar 30 '21 at 09:24
  • @cly Program is running on Windows 10 Enterprise Version 20H2 – TomB Mar 30 '21 at 13:59
  • How do you start the your service app when you try to debug it? From _Service Manager_ or from _Visual Studio_? – cly Mar 30 '21 at 14:22
  • @cly From within Visual Studio – TomB Mar 31 '21 at 07:23
  • Take a look at this thread: https://stackoverflow.com/questions/15604014/no-console-output-when-using-allocconsole-and-target-architecture-x86 The suspected root of the problem is not the same but read the solutions: they suggest reopenig console to workaround VS redirecting behaviour. Others get to the same solution in C++ too: https://stackoverflow.com/questions/32185512/output-to-console-from-a-win32-gui-application-on-windows-10 Worth a try. – cly Mar 31 '21 at 08:08
  • Tried all 3 possibilities from above mentioned solution. No success. When calling AllocConsole() the new console window shows for a split of a second and then disappears – TomB Mar 31 '21 at 12:48

0 Answers0