【问题标题】:How can I start a process in the background?如何在后台启动进程?
【发布时间】:2015-07-25 15:00:01
【问题描述】:

我似乎无法在 Google 或 StackOverflow 上找到答案。

如何在后台(在活动窗口后面)启动进程?就像,当进程启动时,它不会中断用户正在使用的当前应用程序。

进程不会在当前应用程序前面弹出,它只会启动。

这是我正在使用的:

Process.Start(Chrome.exe);

Chrome 启动时会在我的应用程序前面弹出。如何让它在后台启动?

我也试过了:

psi = new ProcessStartInfo ("Chrome.exe");
psi.UseShellExecute = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(psi);

但是和上一个完全没有区别。

谢谢。

【问题讨论】:

  • 背景是指活动窗口的后面吗?
  • 是的,抱歉,英语不是我的母语。
  • 这可能会有所帮助:stackoverflow.com/questions/2027536/…
  • 这不是将Chrome 启动后置于后台吗?如何让它在后台启动?

标签: c# .net windows background-process


【解决方案1】:

试试这个:

 Process p = new Process();
        p.StartInfo = new ProcessStartInfo("Chrome.exe");
        p.StartInfo.WorkingDirectory = @"C:\Program Files\Chrome";
        p.StartInfo.CreateNoWindow = true;
        p.Start();

另外,如果这不起作用,请尝试添加

p.StartInfo.UseShellExecute = false;

【讨论】:

  • 我必须添加 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 以使其完全隐藏。
  • 仅使用 p.StartInfo.CreateNoWindow = true;没用,但 p.StartInfo.UseShellExecute = false;完成了这项工作。
【解决方案2】:

下面的代码应该做你需要的:

class Program
{
    static void Main(string[] args)
    {
        var handle = Process.GetCurrentProcess().MainWindowHandle;
        Process.Start("Chrome.exe").WaitForInputIdle();
        SetForegroundWindow(handle.ToInt32());
        Console.ReadLine();
    }

    [DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int hWnd); 
}

【讨论】:

  • 这仍然使 Chrome 在我的活动窗口前面弹出并使其成为我当前的活动窗口。没有错误。但它仍然没有在后台启动它。结果和以前一样。
  • @RhythmHeavenFever,我已经编辑了代码,当然,在开始其他进程之前,您需要处理当前进程。再试一次。
猜你喜欢
  • 1970-01-01
  • 2010-11-14
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 2019-10-25
相关资源
最近更新 更多