Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid processes to steal focus from C# application?

Tags:

c#

focus

process

I'm having some problems starting multiple processes sequentially in C#. These processes I'm trying to start spend just 30 seconds more or less to finish his procedure. And I don't want these processes stealing focus from any app. I've tried two approaches (the following codes are into a looping statement):

myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = "References\\InclinacaoHGrid.exe";
myProcess.StartInfo.CreateNoWindow = true;

myProcess.StartInfo.Arguments = i.ToString() + " " + j.ToString() + " " +
                                        valA + " " + valS + " " + valP + " " + pathNodes;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
myProcess.Start();
myProcess.WaitForExit();

And using Interaction.Shell with the parameter "AppWinStyle.MinimizedNoFocus":

int pid = Interaction.Shell("References\\InclinacaoHGrid.exe " + i.ToString() + " " + j.ToString() + " " +
                                        valA + " " + valS + " " + valP + " " + pathNodes, AppWinStyle.NormalNoFocus,true);

These two approaches didn't work to me because the processes (or the action of start and end the process) are stealing the focus from any application. I can't do anything when my application is running.

like image 540
William Cesar Farias Avatar asked Nov 04 '22 11:11

William Cesar Farias


1 Answers

Change:

myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

Into:

myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

You don't seem to need UseShellExecute in your example, as you're running an executable directly and thus do not need the shell in this case.

Please note however that changing UseShellExecute to false does have the following effects:

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used only by the process that is started and has meaning only within the context of the new process. When UseShellExecute is false, the FileName property must be a fully qualified path to the executable.

And don't use myProcess.WaitForExit(); unless you really do want to wait for the process to exit.

The WaitForExit() overload is used to make the current thread wait until the associated process terminates. This method instructs the Process component to wait an infinite amount of time for the process and event handlers to exit. This can cause an application to stop responding.

Also see this for more information about the ProcessWindowStyle enumeration and possible values.

like image 101
Johannes Kommer Avatar answered Nov 14 '22 05:11

Johannes Kommer