Console Application without showing console window

Asked

Viewed 3,375 times

5

I have a Console Application in which I use to run routines of my system. I am running this console from the Task Scheduler, every time it runs it flashes on the screen. It opens, executes the process and closes. I don’t want the console to appear on the screen, I just want it to run.

How do I do that?

Note: The application needs to be a Console Application, so the option to change the properties from PROJ to Windows Application is out of the question.

  • Why not create a windows service?

  • 1

    This is an interesting link. http://stackoverflow.com/a/3571628/4190610

  • @Marconi The company needs it to be a console app

  • @jbueno I already tested the test link example, and even then the Console flashes on the screen.

  • @Jonathanbarcela I get it.

  • 4

    @Jonathanbarcela because the company needs it? These requirements seem to be artificial.

  • @Bigown I don’t think that needs interfere with solving the problem, but come on. I am trying to do what I was given, create a console application for the execution of the administrative routines of the system. This Console app will be used by another system, which monitors these routines (in more than one module) in which picks exceptions, etc... that are shown in the control room. For this reason it cannot be a service and also needs to run in background.

  • You can recompile the code and switch to Windows Forms application?

  • 1

    @Jonathanbarcela It matters because the whole solution is wrong.

  • @Guilhermenascimento As quoted in the question, no. :/

  • 1

    @bigown I have to work with the tools that are available to me, unfortunately.

  • @Jonathanbarcela in this case does not have because you will create the application, can create the way you want. But okay, want to do Gambi, do,

  • 1

    This is strange and very confusing :( - What do you think differs in the end result o Windows Forms application of Console for you?

  • Is not flashing the console also a requirement? If it is, it seems that they are asking incompatible things. There is no one in the company with whom you can debate this matter?

  • 3

    I think the problem lies in the question. The real problem is not the application having the window, but how to make the scheduler run without showing this window. Have you tried the option to "run hidden" from the scheduler? http://i.stack.Imgur.com/lsued.jpg Alternatively, schedule with cmd /c start /min SEUAPP, is not the same thing as hidden, but is minimized in Taskbar.

Show 10 more comments

5 answers

6


According to the answers I found in Soen you can try:

  1. Freeconsole:

    [DllImport("kernel32.dll")]
    public static extern bool FreeConsole();
    
  2. Use ProcessStartUpInfo.CreateNoWindow = true;

    public static void main(string[] args)
    {
       Process p = new Process("MyApp");
       ProcessStartUpInfo pinfo = new ProcessStartUpInfo();
       p.StartupInfo = pinfo;
       pinfo.CreateNoWindow = true;
       pinfo.ShellExecute = false;
    
       p.RaiseEvents = true;
    
       AutoResetEvent wait = new AutoResetEvent(false);
       p.ProcessExit += (s,e)=>{ wait.Set(); };
    
       p.Start();
       wait.WaitOne();
    }
    
  3. Alternatively you can compile Windows Forms application, understand little of . net, but as far as I know this implies little in the execution and for your case seems to be a good exit.

  • 1

    @Marcosregis Only one example is with Windows Forms Application and the PA didn’t explain why he couldn’t use it. Note that I cited 3 examples, the second being alternative and my answer goes beyond the AP, it intends to help other people with the same doubt and who do not have this problem with Windows Forms Application :) I hope you understand my justification

1

Console or Windows Forms are projects that expect user interaction. Because the task you need to perform does not depend on interaction, as you are trying to hide the Console. It is recommended that you use a service.

Or use:

 using System.Runtime.InteropServices;
 [DllImport("kernel32.dll")]
 static extern IntPtr GetConsoleWindow();

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

 const int SW_HIDE = 0;
 const int SW_SHOW = 5;

 var handle = GetConsoleWindow();

 // Hide
 ShowWindow(handle, SW_HIDE);

 // Show
 ShowWindow(handle, SW_SHOW);

-1

Use Widnowsservice for these routines, the Service will be installed on your server or machine and will be running in the background, nor need trigger, you put all this information in the Timer within the project.

-2

You must perform the task on an account other than the one the user is logged in to. So nothing will appear.

  • 1

    This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication. - From Review

  • 2

    @jbueno I understand that is an answer, and it is 100% correct. However, it is suggested that lsalamon elaborate the text better explaining at least how the user should proceed to perform a task as a different user, to avoid removal by low quality. In the Superuser has a similar but elaborate response.

  • What do you mean, man, you don’t answer the guy’s question

  • 1

    @Cypherpotato Question: "I don’t want the console to appear on the screen, I just want you to do it. How do I do it?" - Answer: "Run using another account and nothing will appear". Explain to me, please, how that doesn’t answer.

  • This is not a favorable response, the user specified the tag C# in the question, then we understand that he wanted an answer in C#, that answer whether or not it has a code, just as it hides the window.

  • 1

    @Cypherpotato I agree, but that’s why they’re saying problem is in question. It has as requirements to be a console application and at the same time start and complete the execution without the console appears if you want to once. This should be handled by the operating system. If it was to be solved in the code, he would have to accept the possibility of the application not being console, do not think?

  • Yeah, there’s that too, so if he put what he wanted in code it would be another story...

  • 3

    Perhaps the c# tag has made some people (like me) think that the answer is incorrect, and in fact it may be correct. But let’s avoid discussions here, this answer is already being discussed at the goal: http://meta.pt.stackoverflow.com/q/4559/3635 - join! @Cypherpotato

Show 3 more comments

-3

It’s Simple you have to go in Project --> Console Properties --> Application --> Output Type and select Windows Application.

  • from the OBS that he added to the question, make the project Windows Application is not feasible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.