3

I have a function for starting processes on a local machine:

public int StartProcess(string processName, string commandLineArgs = null)
{
    Process process = new Process();
    process.StartInfo.FileName = processName;
    process.StartInfo.Arguments = commandLineArgs;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.ErrorDialog = false;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.Start();
    return process.Id;
}

It is supposed to start the process without opening a new window. Indeed, when I test it with timeout.exe no console window is opened. But when I test it with notepad.exe or calc.exe their windows still open.

I saw online that this method works for other people. I'm using .NET 4.0 on Windows 7 x64.

What am I doing wrong?

Dina
  • 1,346
  • 1
  • 15
  • 35
  • What is the source where it works for other people? – Alvin Wong Oct 23 '12 at 06:53
  • 1
    I don't think that there's a general way to launch another executable and then *prevent* that executable from deciding to open its own windows. – Damien_The_Unbeliever Oct 23 '12 at 06:59
  • I agree with @Damien_The_Unbeliever. Generally if a process decide to open a window, it requires user interaction, so it usually won't function without a window. Unlike console applications, where you can programmatically read and write to the standard input/output. – Alvin Wong Oct 23 '12 at 07:07
  • http://stackoverflow.com/questions/3011209/c-sharp-launch-invisible-process-createnowindow-windowstyle-not-working. Chris Schmich says that it works on his machine. @AlvinWong – Dina Oct 23 '12 at 07:14
  • @Damien_The_Unbeliever Thanks. I will stick to hiding only console applications. – Dina Oct 23 '12 at 07:32

3 Answers3

3

The CreateNoWindow flag applies to Console processes only.

See here for the details:

Secondly applications can ignore the WindowStyle argument - it has effect the first time the new application calls ShowWindow, but subsequent calls are under the control of the application.

Ben
  • 34,935
  • 6
  • 74
  • 113
2

Following program will show/hide the window:

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

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

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

    static void Main()
    {
        // The 2nd argument should be the title of window you want to hide.
        IntPtr hWnd = FindWindow(null, "Untitled - Notepad");
        if (hWnd != IntPtr.Zero)
        {
            //ShowWindow(hWnd, SW_SHOW);
            ShowWindow(hWnd, SW_HIDE); // Hide the window
        }
    }
}

Source: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/1bc7dee4-bf1a-4c41-802e-b25941e50fd9

Azodious
  • 13,752
  • 1
  • 36
  • 71
  • 2
    What do *you* see when you look at line 4 of the OPs `StartProcess` method? (Hmm. Maybe I miscounted, or maybe I subconsciously started counting from 0, but hopefully the point still works) – Damien_The_Unbeliever Oct 23 '12 at 06:51
  • @Damien_The_Unbeliever: Yes, missed it. – Azodious Oct 23 '12 at 07:14
  • +1 this seems to be the most powerful method e.g. it works on calc where other methods fail. Besides findwindow , you can get a process's handle. Then as you do, hide the process` IntPtr blah=p.MainWindowHandle; ShowWindow(blah, SW_HIDE);` – barlop Apr 26 '16 at 00:46
1

You need to remove the process.StartInfo.UseShellExecute = false

    public int StartProcess(string processName, string commandLineArgs = null)
    {
        Process process = new Process();
        process.StartInfo.FileName = processName;
        process.StartInfo.Arguments = commandLineArgs;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.ErrorDialog = false;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        return process.Id;
    }
Amirshk
  • 8,170
  • 2
  • 35
  • 64
  • did you test this on calc? – barlop Apr 24 '16 at 14:09
  • testing with cmd.exe, (and not a console application project). I agree with `You need to remove the process.StartInfo.UseShellExecute = false` but oddly enough https://msdn.microsoft.com/en-us/library/system.diagnostics.processwindowstyle(v=vs.110).aspx says "To use ProcessWindowStyle.Hidden, the ProcessStartInfo.UseShellExecute property must be false." – barlop Apr 25 '16 at 06:08