14

I can't seem to find an answer on Google or here on StackOverflow.

How can I start a process in background (behind the active window)? Like, when the process starts, it will not interrupt the current application the user is using.

The process won't pop out in front of the current application, it will just start.

This is what I'm using:

Process.Start(Chrome.exe);

Chrome pops up in front of my application when it's started. How can I make it start in background?

I've also tried:

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

But there's no difference at all from the previous one.

Thanks.

Barett
  • 5,826
  • 6
  • 51
  • 55
Robert Malansangan
  • 153
  • 1
  • 1
  • 6

2 Answers2

30

Try this:

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

Also, if that doesn't work, try adding

p.StartInfo.UseShellExecute = false;
Tayla Wilson
  • 464
  • 4
  • 12
  • 6
    I had to add p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden to make it completely hidden. – Erwin Mar 10 '17 at 12:31
  • only using p.StartInfo.CreateNoWindow = true; didn't worked but p.StartInfo.UseShellExecute = false; did the work. – Rahul Sonone Mar 27 '20 at 06:07
4

Below code should do what you need:

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); 
}
Vitaliy Tsvayer
  • 765
  • 4
  • 14
  • 2
    That still made Chrome pop out in front my active window and made it my current active window. No errors. But it still didn't start it in background. Result was the same as before. – Robert Malansangan Jul 25 '15 at 15:36
  • @RhythmHeavenFever, I have edited code, you need to get handle of your current process before starting other process of course. try it again. – Vitaliy Tsvayer Jul 25 '15 at 16:34