Open explorer window and wait for it to close

前端 未结 2 1714
半阙折子戏
半阙折子戏 2020-12-21 08:21

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

相关标签:
2条回答
  • 2020-12-21 08:47

    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.

    0 讨论(0)
  • 2020-12-21 08:55

    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.

    0 讨论(0)
提交回复
热议问题