I have a console application I'm using to run scheduled jobs through windows scheduler. All the communication to/from the application is in email, event logging, database logs. Is there any way I can suppress the console window from coming up?
Asked
Active
Viewed 1.2k times
6 Answers
45
Sure. Build it as a winforms app and never show your form.
Just be careful, because then it's not really a console app anymore, and there are some environments where you won't be able to use it.

Joel Coehoorn
- 399,467
- 113
- 570
- 794
-
Any way I can do it inside the existing project so I don't have to migrate stuff? – Jeff Jun 01 '09 at 13:51
-
16Right-click the project, go to Properties, and in the form that pops up, change it from a Console app to a WinForms app, close, and recompile. – Chris Doggett Jun 01 '09 at 13:53
-
You can also delete the Form class and never instantiate it – Jader Dias Oct 04 '09 at 21:49
-
1@ChrisDoggett - Thanks for the awesome tip! I had no idea you could switch between those project types so easily! – Dan Esparza Oct 29 '13 at 13:45
10
Borrowed from MSDN (link text):
using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
...
//Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.
IntPtr hWnd = FindWindow(null, "Your console windows caption"); //put your console window caption here
if(hWnd != IntPtr.Zero)
{
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
}
if(hWnd != IntPtr.Zero)
{
//Show window again
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}

MPritchard
- 7,031
- 7
- 28
- 40
2
Simply configure the Scheduled Task as "Run whether user is logged on or not".

Peter Meinl
- 2,566
- 25
- 39
2
It's a hack, but the following blog post describes how you can hide the console window:
http://expsharing.blogspot.com/2008/03/hideshow-console-window-in-net-black.html

Philippe Leybaert
- 168,566
- 31
- 210
- 223
-
Very nice find. I will use this more than once at some point in the future. Thanks Philippe. BTW, I don't think invoking user32.dll is a hack if its not in the .Net framework. It's just "how it's done." – philologon Mar 02 '13 at 04:22
2
Schedule the task to run as a different user than your account and you won't get a window popping up . . .

Wyatt Barnett
- 15,573
- 3
- 34
- 53
1
Why don't you make the application a Windows Service?

Badaro
- 3,460
- 1
- 19
- 18
-
4No- scheduled jobs != windows service. Unless he's using the e-mail to kick off the processing, in which case a service is a better option. – Joel Coehoorn Jun 01 '09 at 13:53
-
2Yes, been down that road before. I use to create scheduled jobs using the service infrastructure, but eventually realized doing it that way results in lots of unnecissary code and difficult upgrades. In short, it recreates an infrastructure the task scheduler has provided for free! – Jeff Jun 01 '09 at 14:00
-
1