22

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?

Jeff
  • 8,020
  • 34
  • 99
  • 157

6 Answers6

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
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
  • 4
    No- 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
  • 2
    Yes, 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
    That's fine but I don't see reasons to downvote Badaro's question. – M.Turrini Jun 01 '09 at 15:01