123

The thing is, I really don't want the console window to show up, but the solution should be running. My point here is, I want to keep the application running in the background, without any window coming up.

How can I do that?

3
  • 2
    Can you give us a little more information regarding what the application should do? It sounds like it should be a service or windows application? With a little knowledge about it's purpose, we'd be able to help a lot more and suggest the best way to solve this. Commented Oct 4, 2010 at 8:27
  • 1
    Its keylogger application. I used windows service but it can not get key states in windows service Commented Oct 4, 2010 at 8:32
  • Does this answer your question? Show/Hide the console window of a C# console application Commented Mar 4, 2022 at 18:23

7 Answers 7

220

Change the output type from Console Application to Windows Application. This can be done under Project -> Properties -> Application in Visual Studio:

alt text

Sign up to request clarification or add additional context in comments.

9 Comments

Console window flashes and goes back. How do i keep the application running with no window at all?
@SOF User: How do you start the application? By double-clicking in Explorer or from the Start Menu?
i have also put Console.ReadLine();
Remove the Console.ReadLine. It doesn't make any sense if you don't have a console window.
@KarolŻurowski: The idea here is that you would use this for apps that either also come with some form of UI (e.g. an icon in the sytem tray) or apps that do a certain task and then exit automatically when done. If you have neither, the app will run in the background until logoff/shutdown or until it is explicitly killed, e.g. using Task Manager.
|
28

Change your application type to a windows application. Your code will still run, but it will have no console window, nor standard windows window unless you create one.

2 Comments

Console window flashes and goes back. How do i keep the application running with no window at all?
You make sure it doesn't terminate. There really is not much else to say without more information. What does the program do? Does it run something in a loop?
14

Instead of Console.Readline/key you can use new ManualResetEvent(false).WaitOne() at last. This works well for me.

Comments

6

You can use user32.dll to achieve this without vscode

    class start
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        static void Main()
        {
            IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
            ShowWindow(h, 0);
            // Do the rest

This still flashes the screen but works good enough

the usings are

using System.Runtime.InteropServices;
using System.Diagnostics;

Note im still new to csharp and not perfect so feel free to comment and corrections!

3 Comments

While the other solution is more popular, I think this solution is underrated! Particularly because I can conditionally show/hide the console window depending on the logic, e.g. by adding compiler directives like "#if DEBUG"
btw, "Process.GetCurrentProcess().MainWindowHandle;" didn't work for me and a solution from stackoverflow.com/a/34440934/1854557 introduces another Windows API call that does the job. (.NET 6 on Windows 10)
Nice solution. Note that technically you also need using System; [this will normally be in use for anything except the most trivial of programs, of course].
3

Maybe you want to try creating a Windows Service application. It will be running in the background, without any UI.

3 Comments

Windows Service can not get Key pressed events
Services are severely limited depending on your application, as of Windows Vista they are forced in to Session 0, and thus can not perform anything in other sessions such as show popup messages.
@Matt "cannot perform anything in other sessions" is not entirely true. Here's a code project for a Service that opens an admin command prompt on the logged-in user's desktop. codeproject.com/Articles/35773/…
3

Change the output type from Console Application to Windows Application,

And Instead of Console.Readline/key you can use new ManualResetEvent(false).WaitOne() at the end to keep the app running.

Comments

0

To compile your application without a console window

csc.exe /target:winexe *.cs

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.