views:

1439

answers:

5

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?

+17  A: 

Sure. Build it as a winforms app and never show your form.

Joel Coehoorn
Any way I can do it inside the existing project so I don't have to migrate stuff?
Jeff
Right-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
Thanks Chris, that worked great!
Jeff
You can also delete the Form class and never instantiate it
Jader Dias
A: 

Why don't you make the application a Windows Service?

Badaro
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
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
That's fine but I don't see reasons to downvote Badaro's question.
Turro
+1  A: 

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
+6  A: 

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
         }
MPritch
+2  A: 

Schedule the task to run as a different user than your account and you won't get a window popping up . . .

Wyatt Barnett