Search code examples
windowsexplorertaskmanager

How to restart explorer like taskmgr?


When you select the explorer process in taskmgr, the button will be "restart" instead of "end task/end process". After clicking restart, explorer shows in "windows process" in taskmgr again as before, but "kill&start" will not. Then I have some questions:

  1. How can I restart explorer like taskmgr?
  2. How could taskmgr know explorer can be restarted and restart it?
  3. Can I make a app that tells taskmgr it can be restarted?

screenshoot: explorer's position in taskmgr, before and after restart, not there after kill&start

My current code for kill&start explorer (C#)

foreach (Process p in Process.GetProcesses())
{
    if (p.ProcessName.ToUpper().Contains("EXPLORER"))
    {
        p.Kill();
        p.WaitForExit();
    }
}
Process.Start("explorer.exe");

And I tried to use cmd

taskkill /f /im explorer.exe
explorer

Solution

  • I debugged taskmgr, and found out that while explorer does get special treatment (taskmgr!WdcApplicationsMonitor::_HandleRestartExplorer) it's just simply calling TerminateProcess.

    Then, it checks the registry for AutoRestartShell (I'm on Windows 11, and it's set by default; can't say for older Windows);

    if set then taskmgr is done; the OS will restart explorer.

    If not set, then it calls WdcRunTaskAsInteractiveUser(L"%windir%\\explorer.exe", nullptr, 9) (undocumented api in wdc.dll, and also the meaning of 9 unknown; seems like a bit flag, and requires further digging) to restart explorer.

    Note: Somehow with taskkill /f /im explorer.exe, explorer doesn't restart even when AutoRestartShell is set.