C# - Launch Invisible Process (CreateNoWindow & WindowStyle not working?)

前端 未结 4 888
遇见更好的自我
遇见更好的自我 2020-12-30 05:34

I have 2 programs (.exe) which I\'ve created in .NET. We\'ll call them the Master and the Worker. The Master starts 1 or more Workers. The Worker will not

相关标签:
4条回答
  • 2020-12-30 05:38

    Your usage of CreateNoWindow/WindowStyle works fine on my system with notepad.exe (e.g. it is hidden but running in the background), so it's probably something the WinForms app is doing. Some ideas:

    Option 1: If you control the WinForms worker process, you can override Control.SetVisibleCore to always hide the form. If you don't want to always hide it, you can pass a command-line argument to it, e.g. /hide that will cause it to be hidden. Example (assuming there's already code-behind for the form):

    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
        }
    
        protected override void SetVisibleCore(bool value)
        {
            // You'd probably want to parse the command line.
            if (Environment.CommandLine.Contains("/hide"))
                base.SetVisibleCore(false);
            else
                base.SetVisibleCore(value);
        }
    }
    

    With this, running MyForm.exe results in a process with a visible form. Running MyForm.exe /hide results in a process with a hidden form. You could pass the /hide argument from your master process, so then normal users running the application will still see it.

    Option 2: You can hide the application after it starts by doing a P/Invoke to ShowWindow. More info on this here. This has the drawback that you can sometimes see the worker window flicker into existence before being hidden. Example:

    class Program
    {
        public static void Main(string[] args)
        {
            ProcessStartInfo psi = new ProcessStartInfo()
            {
                FileName = @"C:\windows\system32\notepad.exe",
            };
    
            Process process = Process.Start(psi);
    
            // Wait until the process has a main window handle.
            while (process.MainWindowHandle == IntPtr.Zero)
            {
                process.Refresh();
            }
    
            ShowWindow(process.MainWindowHandle, SW_HIDE);
        }
    
        const int SW_HIDE = 0;
    
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    }
    
    0 讨论(0)
  • 2020-12-30 05:41

    Since my reputation is too low, i can only post my answer, ubable to comment Chris Schmich's answer.

    Before start process, you can set WindowStyle = ProcessWindowStyle.Minimized to avoid window flicker.

    Here is my code:

    private const int SW_HIDE = 0;
    
    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
    
    public int RunWithoutWindow(string exePath)
    {
        var startInfo = new ProcessStartInfo(exePath)
        {
            WindowStyle = ProcessWindowStyle.Minimized
        };
    
        var process = Process.Start(startInfo);
    
        while (process.MainWindowHandle == IntPtr.Zero)
        {
            process.Refresh();
        }
    
        ShowWindow(process.MainWindowHandle, SW_HIDE);
    
        return process.Id;
    }
    

    Finally thanks good answer provided by Chris Schmich, while ProcessWindowStyle.Hidden sometimes not work.

    0 讨论(0)
  • 2020-12-30 05:45

    You have to add base.Visibility = Visibility.Hidden; in Win Form application constructor.

    0 讨论(0)
  • 2020-12-30 05:55

    The problem is with UseShellExecute = false, set this to true and the process will be started as hidden. Using the shell to start the process understands how to make the application hidden, where as starting the process directly with UseShellExecute = false starts the process directly, and as Chris Schmich mentioned, you'd have to handle hiding the window from inside the client application. This might be more desirable if you want the option of running the application manually for debugging or testing purposes.

    0 讨论(0)
提交回复
热议问题