0

I have a basic app that asks user to 'Press any key or Enter to exit'. The purpose of implementing my app this way is to have it running till the system shuts down or the user logs out. But I want to have it invisible so the user doesn't accidentally close it when pressing a key intended for another program. The structure of my program is:

using System;

namespace myApp
{
  class Program
  {
     private static void Main()
     {
       // Some code here

       // do not close until closed manually
       Console.WriteLine("Press any key to exit");
       Console.ReadKey();
     }

      public static void Method1 ()
      {
         // some code here
      }

      public static void Method2 ()
      {
         // some code for method 2 here
      }
    }
 }

In a similar post, people are suggesting solutions that are either little advanced or do not work for me, e.g.

  1. Changing into a Windows service application,
  2. Running the .exe file of the program as a task,
  3. Outputting this console app as a Windows app,
  4. Starting the VS project as a Windows Form application then not calling any forms, etc.

But this does not work for me. Let me describe why. I have tried options 3 and 4 above, but my app completely disappears/closes, I check the task manager thinking it is may be running in the background but it is not there and that means it closes straight away.

I feel changing the app to a windows service will complicate things for my ClickOnce app. As for 2, people have suggested to open the program as a task and hide the window, but I am not sure how I can do this in the code itself without calling it externally as .exe application. Thank you in advance.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ros
  • 39
  • 9
  • Possible duplicate of [Show/Hide the console window of a C# console application](https://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application) – npo Dec 18 '18 at 22:38
  • 5
    This doesn't make any sense. You don't want the app to show: use a Windows Service. There's no other way you can ensure it is always open and closed by its own – Camilo Terevinto Dec 18 '18 at 23:11
  • @CamiloTerevinto wow fantastic editing skills there, thank you for making my question more readable. But one thing you probably need to realise is that not everyone has the same skill level as you, and I am sure you were not as good at programming/coding when you started out. So if you want to share the knowledge please do in a positive way. Thanks again. – Ros Dec 19 '18 at 14:45

2 Answers2

3

You should develop your application as Windows Service.

Alternatively you can start your exe by Task Scheduler as background task. If you set it as run at startup there will be no ui showed even if it is a form application.

rca
  • 196
  • 1
  • 9
  • thank you, I will try that. Thank you so much for answering my question so patiently, much appreciated. – Ros Dec 19 '18 at 15:22
1

This is related to operating system, you can run your console program as follows:

Assuming you compile your program as program.exe, you can run this in command line.

START /B program.exe > log.txt

https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background

OKEEngine
  • 888
  • 11
  • 28