Benny F
Benny F

Reputation: 535

C# process start focus issue


When I start a new process, it automatically gets the focus. how can I prevent it from getting the focus or instead get back the focus to my application?

here is the code I'm using:

string path = @"c:\temp\myprocess.exe";  
ProcessStartInfo info = new ProcessStartInfo(path);  
info.WorkingDirectory = path;  
Process p = Process.Start(info);  

I just need the executed process not to get the focus.

Thank you very much,
Adi Barda

Upvotes: 4

Views: 6055

Answers (4)

Carlos Mtz
Carlos Mtz

Reputation: 151

What i´ve done was to wait a little delay until the other application was succesfully loaded, then focus my application window.

//Test window
const string strCmdText = "/C cd C:\\sqlcipher";
Process.Start("CMD.exe", strCmdText);

//Delay
int liMilliseconds = 50;
Thread.Sleep(liMilliseconds);

//Code to bring window to front
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;

Upvotes: 0

Dyppl
Dyppl

Reputation: 12381

If you don't need to show the process at all, try this:

string path = @"c:\temp\myprocess.exe";
ProcessStartInfo info = new ProcessStartInfo(path);
info.WorkingDirectory = path;
info.WindowStyle = ProcessWindowStyle.Hidden;

Or set WindowStyle to ProcessWindowStyle.Minimized if you want it visible but minimized, as Uwe Keim said.

Upvotes: 4

scott
scott

Reputation: 3071

can you do

myForm.Focus();

where myForm is the form on your main application

Upvotes: 0

Uwe Keim
Uwe Keim

Reputation: 40736

Maybe setting the WindowStyle property to Minimized can help.

Upvotes: 4

Related Questions