C# - 启动隐形流程(?CreateNoWindow和放大器; WindowStyle不工作)放大器、流程、工作、CreateNoWindow

2023-09-03 01:54:42 作者:爷丶有爷的范°

我有2个程序(.exe)的我在.NET中已经创建。我们会打电话给他们的主人和工人。主开始1个或更多的工人。工人将不可以由用户与之交互,但它是一个WinForms应用程序接收命令,并运行基于它接收来自主命令的WinForms组成部分。

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 be interacted with by the user, but it is a WinForms app that receives commands and runs WinForms components based on the commands it receives from the Master.

我想工人的应用程序为运行完全隐藏(除了在显示过程中的任务管理器了)。我以为我可以在 StartInfo.CreateNoWindow 和 StartInfo.WindowStyle 属性,但我还是看到了Client.exe形式的窗口和组件做到这一点。然而,它并不在任务栏显示

I want the Worker app to run completely hidden (except showing up in the Task Manager of course). I thought that I could accomplish this with the StartInfo.CreateNoWindow and StartInfo.WindowStyle properties, but I still see the Client.exe window and components in the form. However, it doesn't show up in the taskbar.

   Process process = new Process
      {
          EnableRaisingEvents = true,
          StartInfo =
              {
                  CreateNoWindow = true,
                  WindowStyle = ProcessWindowStyle.Hidden

                  FileName = "Client.exe",
                  UseShellExecute = false,
                  ErrorDialog = false,
              }
      };

什么我需要做的,让Client.exe运行,但显示不出来?

What do I need to do to let Client.exe run, but not show up?

推荐答案

您使用 CreateNoWindow / WindowStyle 的作品罚款我的系统的notepad.exe(例如,它是隐藏的,但在后台运行),因此它可能是东西WinForms应用程序在做什么。一些想法:

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:

选项1:如果你控制的WinForms工作进程,则可以覆盖Control.SetVisibleCore总是隐藏表单。如果您不想总是隐藏它,你可以通过一个命令行参数给它,如 /隐藏,这将导致它被隐藏。示例(假设有已经code隐藏表单):

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);
    }
}

通过此,运行 MyForm.exe 产生一个可见的形式的过程。运行 MyForm.exe /隐藏导致进程与隐藏的表单。你可以从你的主进程通过 /隐藏参数,所以后来运行的应用程序正常的用户仍然会看到它。

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.

选项2:它开始做一名P后,您可以隐藏应用程序/ Invoke来的 的ShowWindow 。在此更多的信息在这里。这样做的缺点是有时你可以看到工人窗口忽悠到存在被隐藏了。例如:

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);
}