I'm sorry if this has been asked but I cannot find an answer to it. I may not be searching the right terms.
I have a console program that needs to be run in the background. The user needs the console to stay open white it runs, but need it to not become the active window when it starts. They would like to continue what they are currently working on and not have to minimize the console every time it starts.
This console application runs multiple times, each time with a new console window. How can I "hide" the console behind the current running task/window?
You can programmatically minimize/restore console windows using below code:
using System;
using System.IO;
namespace ConsoleApplication1
{
class Class1
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_MINIMIZE = 6;
private const int SW_MAXIMIZE = 3;
private const int SW_RESTORE = 9;
[STAThread]
static void Main(string[] args)
{
IntPtr winHandle =
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(winHandle, SW_MINIMIZE);
System.Threading.Thread.Sleep(2000);
ShowWindow(winHandle, SW_RESTORE);
}
}
}
If you're using Process.Start to start the console app, better use this:
System.Diagnostics.ProcessStartInfo process= new
System.Diagnostics.ProcessStartInfo(@"MyApplication.exe");
process.WindowStyle=System.Diagnostics.ProcessWindowStyle.Minimized;
process.UseShellExecute=false; // Optional
System.Diagnostics.Process.Start(process);
I don't know of a way to prevent the console window from ever displaying. The best approach in a console app is to hide the window, immediately after the app loads. The visual effect is a quick opening of a console window, then it disappears. Maybe not optimal.
A better approach might be to use a Winexe, instead of a console app. Inside the Windows app (maybe a winforms app), never instantiate the first form. Therefore nothing is ever displayed.
This prevents user interaction, but from your description, it sounds like that's what you want.
But I'm not sure you have control over the "console app".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With