0

I have about 15 different console apps on my local PC and they are running with different time periods as scheduled tasks.

Since I am using this computer as personal usage (such as surfing on YouTube or Watching Movies) They are jumping on my screen but I have to always minimize them manually.

My goal is, I want them to first appear (which is already doing) and lose automatically focus after a couple of seconds.

Is it possible with console apps on Windows?

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158

1 Answers1

3

If you want to minimize console window, you can use WinApi

const Int32 SW_MINIMIZE = 6;

[DllImport("Kernel32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern IntPtr GetConsoleWindow();

[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow([In] IntPtr hWnd, [In] Int32 nCmdShow);

private static void MinimizeConsoleWindow()
{
    IntPtr hWndConsole = GetConsoleWindow();
    ShowWindow(hWndConsole, SW_MINIMIZE);
}

Usage:

static void Main(string[] args)
{
    Console.WriteLine("Starting foo...");
    Thread.Sleep(1000); // hold console for a second on the screen
    MinimizeConsoleWindow();
    Console.ReadKey();
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459