Wait for the process to start.

So what I'm trying to do is launch explorer from my program, and then pop up my application back to Explorer or just launch explorer behind my application ...

I currently have an explorer, after which I have actions to bring my application to the foreground, but the explorer may take a few seconds, and this ruined the whole chain of events.

This is what I am doing now:

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 issue will be greatly appreciated.

Edit: I'm looking for some kind of event that I can call TopMost or my minimized / show / normal method after loading explorer.

The program creates a project directory with all the necessary documents for each type of project, this directory pops up in the explorer.

This means a change in the quality of life for users who want to create 10 or 20 projects in one sitting.

+6
source share
1 answer

Ugly way

Usually, when you wait for the download process to complete, you call

 Process.WaitForInputIdle() 

From MSDN :

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

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

The workaround was to start the Sleep process, say 250-500 ms, and then find a new Process using some kind of WaitForInputIdle hack, and then call WaitForInputIdle on that Process .

Alternative

If you are ready to run explorer.exe minimized, you can do this as follows:

 ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "explorer.exe"; psi.Arguments = "/separate"; psi.UseShellExecute = true; psi.WindowStyle = ProcessWindowStyle.Minimized; Process.Start(psi); 
+6
source

Source: https://habr.com/ru/post/952210/


All Articles