3

I have a program that is opening an explorer window to a certain folder but i want to perform an action right after the explorer window is closed, but if I use the following code:

Process proc = Process.Start("explorer.exe", "D:\\");
proc.WaitForExit();

It is opening the explorer window as desired but the WaitForExit command has no effect and it just goes right past it.

Is there a different way of opening the explorer window that will be able to let me know when it is closed by the user?

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
Brian Tacker
  • 1,091
  • 2
  • 18
  • 37
  • The problem is that there is one explorer.exe for all windows and the main desktop. Not sure how to fix it, but maybe you can enumerate the windows and find the right title, then wait for that window to close? Pretty error-prone though. – Michael Kennedy May 27 '11 at 18:41
  • See this thread [from msdn](http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8adbf55a-cae3-4168-88a9-56153c540e16). It looks like you can't if you are running explorer as your shell. – Bala R May 27 '11 at 18:42
  • I can tell you that the wait for exit part isn't working because it's not really starting a new process. There's a setting somewhere to 'Open windows in a new process', or there used to be. Good luck. – Mark Allen May 27 '11 at 18:49

2 Answers2

7

The problem is explained pretty well at The Old New Thing:

The reason that WaitForSingleObject returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your WaitForSingleObject returns immediately.

He offers a couple solutions you could probably use (with some heavy use of PInvoke), like using something like this.

In the end it might just be easier for you to use some other type of file browser maybe from a C# library somewhere that you have more control over, rather than explorer.

Sogger
  • 15,962
  • 6
  • 43
  • 40
  • +1 The real problem is that you need to be using a component that is designed to be under your control and not trying to piggyback the system wide shell process. – David Heffernan May 27 '11 at 19:01
3

Cannot regenerate the error. Just tried this:

Process.Start("explorer.exe", "D:\\").WaitForExit();

and it blocks the current thread and waits until I close the explorer windows. Make sure that you're not executing the command on another thread than the one you want to block. Also make sure that you set every instance of a window to start a new instance of explorer.exe via importing below .reg file:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"

You'll need to restart your computer for this to take effect.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80