67

I have a console application written in C# that is scheduled to run every 15 minutes or so using the built-in Windows Task Scheduler.

Every time it runs, the black console box pops up for the duration of its execution and then closes. I am not writing anything to the console. Is there a way to make this run in the background?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
etoisarobot
  • 7,684
  • 15
  • 54
  • 83
  • Not from the console app itself, I think you should ask on Superuser how to configure this in the Scheduler. – H H Apr 21 '10 at 20:25
  • Check [this](https://www.youtube.com/watch?v=PzrTiz_NRKA) out if you look for .net core 3 solution. – Ognyan Dimitrov Oct 08 '19 at 07:55

8 Answers8

193

Project > Properties> Application tab > change Output type to "Windows application".

No more console window.


Update for .NETCore aka .NET5+: edit the project file and change OutputType to WinExe.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 9
    I had ran into the same issue, and this answer helped me far more than the accepted one. – user17753 Jul 11 '12 at 17:34
  • 1
    agreed - this is a great answer - really simple and works perfectly – Peter Munnings Sep 07 '12 at 15:15
  • 3
    Perfect solution to this question, as I have already created my console project, I just had to change the property, instead of the accepted answer which requires the creation of a new project, perfect, thank you! – Jeffrey L. Roberts Nov 10 '13 at 01:22
  • 2
    must say this solution was simpler and better. Thanks – Mana Dec 09 '13 at 13:15
  • This works great at hiding the console window, but defeats my "press any key to close" functionality. In fact, because I rely on that the console app just closes after running for a few seconds. Is there a way to keep it going? – Doctor Blue Mar 06 '14 at 12:53
  • this should be the Best Answer, unless the first one is trying to do something which this cannot.Anyway I will prefer this.. – Raulp Nov 17 '14 at 08:55
  • This is quite an elegant solution as far as I'm concerned. It allows you to use the console for debugging purposes while working and then easily disable the console when actually deploying the program. – James Coyle Nov 18 '14 at 20:43
56

Easy!

It seems hard to believe, but it works as a charm. I have used this for some setup projects, when you want to perform custom tasks with no signs of it.

  • Create the project as a Windows application project (this is the hard part).
  • Never make calls to any form. Just keep on in exactly as in your console application

    class Program
    {
        static void Main(string[] args)
        {
            // Just don't call Application.Run(new frmMain(args));
    
            // ... your code
        }
     }
    

This is because windows application projects are no really different than console, except because of the first form and references. It is totally hidden execution. Try it!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Dolz
  • 2,403
  • 1
  • 18
  • 23
12

You can use the Windows API to minimize the console box. Otherwise you can make it a Windows EXE file that does not actually load a form and call System.Windows.Forms.Application.Run().

Code to minimize the console:

[DllImport( "user32.dll" )]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public const int SW_SHOWMINIMIZED = 2;

IntPtr winHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(winHandle, SW_SHOWMINIMIZED);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JDMX
  • 1,489
  • 9
  • 22
  • 2
    I was getting ready to post this! To hide the window: public const int SW_HIDE = 0; To restore: public const int SW_RESTORE = 9; – JohnForDummies Apr 21 '10 at 20:55
9

What about implementing the app into a windows service? You can set the interval to 15 mins and run the operation in the timer_tick.

Erup
  • 522
  • 5
  • 13
6

If you already have a Windows Console app created you can simply change the Output type of your app to Windows Application.

Under your project: Go to Properties > Application Select "Windows Application" as the Output type.

This would be the least impact and you can keep your Windows Task Scheduler running the same task.

Chanrith
  • 113
  • 1
  • 3
3

If it doesn't write anything to the console you could make it a service. http://msdn.microsoft.com/en-us/library/9k985bc9%28VS.80%29.aspx

nportelli
  • 3,934
  • 7
  • 37
  • 52
2

It will only show up if it's scheduled to run as the same user that's currently logged in. Create another user on the machine with a ridiculously long password, set it to be an Administrator (only if needed) and schedule the task to run as that user.

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
0

This is easy. Set the task to run under an account that is not your login account.

37Stars
  • 2,489
  • 20
  • 23