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?

4

6 回答 6

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.

于 2009-06-01T13:51:18.253 回答
10

借自 MSDN(链接文本):

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
         }
于 2009-06-01T13:58:17.960 回答
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

于 2009-06-01T13:55:45.343 回答
2

安排任务以与您的帐户不同的用户身份运行,您不会弹出窗口。. .

于 2009-06-01T14:06:01.907 回答
2

只需将计划任务配置为“无论用户是否登录都运行”。

于 2012-10-04T20:09:15.570 回答
1

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

于 2009-06-01T13:52:28.127 回答