Console Application without displaying console window

5

I have a Console Application in which I use to run routines of my system. I'm running this console from the Task Scheduler, every time it runs, it blinks on the screen. Opens, executes the process, and closes. I do not want the console to appear on the screen, I just want it to run.

How do I do this?

Note: The application must be a Console Application, so the option to change the properties of the PROJ for Windows Application is out of the question.

    
asked by anonymous 13.11.2015 / 19:42

5 answers

6

Depending on the answers I found in SOen you can try:

  • FreeConsole:

    [DllImport("kernel32.dll")]
    public static extern bool FreeConsole();
    
  • Use ProcessStartUpInfo.CreateNoWindow = true;

    public static void main(string[] args)
    {
       Process p = new Process("MyApp");
       ProcessStartUpInfo pinfo = new ProcessStartUpInfo();
       p.StartupInfo = pinfo;
       pinfo.CreateNoWindow = true;
       pinfo.ShellExecute = false;
    
       p.RaiseEvents = true;
    
       AutoResetEvent wait = new AutoResetEvent(false);
       p.ProcessExit += (s,e)=>{ wait.Set(); };
    
       p.Start();
       wait.WaitOne();
    }
    
  • As an alternative you can compile Windows Forms application , I understand little of .NET, but as far as I know this does not imply execution and for your case it seems to be a good way out. p>

  • 13.11.2015 / 20:44
    1

    The Console or Windows Forms are projects that expect user interaction. Since the task you need to perform does not depend on interaction, you are trying to hide the Console. It is recommended that you use a service.

    Or use:

     using System.Runtime.InteropServices;
     [DllImport("kernel32.dll")]
     static extern IntPtr GetConsoleWindow();
    
     [DllImport("user32.dll")]
     static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
     const int SW_HIDE = 0;
     const int SW_SHOW = 5;
    
     var handle = GetConsoleWindow();
    
     // Hide
     ShowWindow(handle, SW_HIDE);
    
     // Show
     ShowWindow(handle, SW_SHOW);
    
        
    13.11.2015 / 23:43
    -1

    Use WidnowsService for these routines, the Service will be installed on your server or machine and will run in the background, nor need a trigger, you put all this information in the Timer within the project.

        
    22.08.2018 / 20:52
    -2

    You must execute the task in an account different from the one that the user is logged in. So nothing will ever show up.

        
    13.11.2015 / 20:39
    -3

    It's Simple you have to go to Project - > ConsoleApp1 Properties - > Application - > Type the output and select Windows Application.

        
    04.08.2017 / 14:46