Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for process start

So what I am attempting to do is start explorer from my program then pop my application back in front of explorer or just start explorer behind my application...

Currently I have explorer starting then I have actions to bring my application to the front but explorer can take a few seconds to start and that messes up my whole chain of events.

This is what I am currently doing:

 Process process = new Process();
 process.StartInfo.FileName = environmentVariable + "\\explorer.exe";
 process.StartInfo.Arguments = !string.IsNullOrEmpty(this.uxMainFolder.Text) ? this.uxMainFolder.Text + "\\" + path2 : Path.Combine("R:\\Project", path2);

 try
 {
      process.Start();

      this.WindowState = FormWindowState.Minimized;
      this.Show();
      this.WindowState = FormWindowState.Normal;
 }
 finally
 {
      process.Dispose();
 }

Any light you can shed on this problem would be much appreciated.

Edit: I'm looking for some event that I can call TopMost or my minimized/show/normal approach on after explorer has been loaded.

The program generates a project directory with all of the required documents for each project type the pops up that directory in explorer.

This is meant as a quality of life change for users wanting to create 10 or 20 projects at one sitting.

like image 904
Mytheral Avatar asked Aug 21 '13 13:08

Mytheral


1 Answers

The Ugly Way

Generally, when waiting for a process to finish loading, you would call

Process.WaitForInputIdle()

From MSDN :

Causes the Process component to wait indefinitely for the associated process to enter an idle state. This overload applies only to processes with a user interface and, therefore, a message loop.

With explorer.exe this will most likely not work, as this process often spawns a child process and instantly dies.

The workaround would be to launch the process, Sleep for say, 250-500ms, then find the new Process using some godawful hack, and then call WaitForInputIdle on that Process.

An Alternative

If you are willing to start explorer.exe minimized, you could do it like this:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "explorer.exe";
psi.Arguments = "/separate";
psi.UseShellExecute = true;
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(psi);
like image 127
Rotem Avatar answered Sep 20 '22 17:09

Rotem