C#流程启动焦点问题

C#流程启动焦点问题,c#,process,focus,C#,Process,Focus,当我启动一个新流程时,它会自动获得焦点。如何防止它获取焦点,或者将焦点返回到我的应用程序 以下是我正在使用的代码: string path = @"c:\temp\myprocess.exe"; ProcessStartInfo info = new ProcessStartInfo(path); info.WorkingDirectory = path; Process p = Process.Start(info); 我只需要执行过程,而不是获得焦点 非常感谢, Adi B


当我启动一个新流程时,它会自动获得焦点。如何防止它获取焦点,或者将焦点返回到我的应用程序

以下是我正在使用的代码:

string path = @"c:\temp\myprocess.exe";  
ProcessStartInfo info = new ProcessStartInfo(path);  
info.WorkingDirectory = path;  
Process p = Process.Start(info);  
我只需要执行过程,而不是获得焦点

非常感谢,
Adi Barda

也许将属性设置为有帮助。

你能做什么

myForm.Focus();

其中myForm是主应用程序上的表单

如果您根本不需要显示流程,请尝试以下操作:

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

或者将WindowsStyle设置为ProcessWindowsStyle.Minimized,如果您希望它可见但最小化,正如Uwe Keim所说。

我所做的是等待一点延迟,直到另一个应用程序成功加载,然后聚焦我的应用程序窗口

//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;

它不起作用,因为在启动线程的那一刻,有一个延迟,直到它的UI显示出来。到那时,你已经把焦点放在你的表单上了,但是外部过程仍然排在最后,并偷走了焦点。为什么这是公认的答案!?它没有回答这个问题。我认为它仍然需要关注,即使它没有显示出来