How to hide a console of a console application from showing up while the program is still in execution

Introduction

While I was working with WCF service application, I came across a situation where I am hosting my WCF service in a console application and I don’t want the console application to show up when my service is up. I fixed it this by Pinvoke a call to FindWindow() to get a handle to your window and then call ShowWindow() to hide the window. I have created a sample code to show how I did this. It is as given below.

using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace TestShowHideConsole
{
    internal class Program
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

        private static void Main(string[] args)
        {
            string consoleWindowTitle = Console.Title;
            IntPtr hWnd = FindWindow(null, consoleWindowTitle);
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 0); /* 0 = SW_HIDE */
            }
            Thread.Sleep(1000*10);/* will show your console after 10 second */
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 1); /*1 = SW_SHOWNORMA */
            }
            ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();/* this will make the window wait till some key is pressed.*/
        }
    }
}

Here in this example, this will open up a console and it will be hidden for 10 seconds and will come up after that.

How to hide a console application If it is running from another application.

If you are running the console application from another application and you want to hide the console window, then you can do like this. This will start your console application but the window will be hidden. You can see your application still running from the task manager. If you cant find your application running in the task manager, then some error is there in your application and you may need to debug it.

Process p = new Process
        {
          StartInfo =  {
                       FileName = YourConsoleaApplicationFileHere,
                       Arguments = "Argument to console application if                     any.",
                       WindowStyle = ProcessWindowStyle.Hidden
/*Or you can use CreateNoWindow = true instead of WindowStyle*/
                       }
         };
p.Start();

Setting Up Kafka

Apache Kafka is a high-throughput, low-latency event processing system designed to handle millions of data feeds per second. It has the c...… Continue reading

Libish Varghese Jacob

Libish Varghese JacobI am a lead engineer at a prominent wind turbine manufacturing firm. My interests span a diverse range, and immersing myself in technology is one of them. This platform serves as my primary knowledge base, where I seek information and insights. Moreover, I utilize this platform to share my experiences and experiments, hoping they may benefit those following a similar path. It's important to note that the suggestions I express here are based on my best knowledge at the time of writing and may not necessarily represent the optimal solutions. I wholeheartedly welcome any comments you may have to improve or refine my ideas. Your feedback is greatly appreciated and valued.