Skip to content

Preload and keep alive Windows' context menu handlers - #19

Merged
Abdullah-Masood-05 merged 2 commits into
Abdullah-Masood-05:prs/2.3.2from
alchemyyy:main
Nov 27, 2026
Merged

Abdullah-Masood-05 merged 2 commits into
Abdullah-Masood-05:prs/2.3.2from
alchemyyy:main

Conversation

@alchemyyy

Copy link
Copy Markdown

Most of the wait before the menu shows is Windows loading the handlers other apps add to it (7-Zip, Windows Terminal, WinMerge and so on). The first right-click after Explorer starts loads all of them, and handlers that run in their own process shut down soon after a menu closes, so the next right-click after a pause has to start them again.

Two new settings, off unless set (the shipped shell.nss turns both on):

  • preload: shortly after Explorer starts, build Windows' menus for a file, a folder and the backgrounds in the background, without showing them.
  • keepalive: keep handler DLLs loaded and out-of-process handlers running between menus.

The app's Behavior page has a switch for each.

@Abdullah-Masood-05

Copy link
Copy Markdown
Owner

Thanks for this, @alchemyyy. It's a well put together change, and the timings in the docs are a nice touch.

I'll build it and test it on my PC this weekend, probably on Sunday, and get back to you after that.

A few things I noticed while reading it, in case you want to look at them before then:

  • Default on. The shipped shell.nss turns both settings on, so everyone who never edited that file gets them with the update. I'd rather ship them off and let people turn them on from the Behavior page, at least at first.
  • A crash at startup could repeat. The preload runs every app's menu handler in Explorer a few seconds after it starts. If one of them crashes, Explorer restarts, preloads again and crashes again. Could the preload leave a marker before it starts and skip itself next time if the last run never finished?
  • Pinned DLLs stay loaded. GET_MODULE_HANDLE_EX_FLAG_PIN can't be undone, so turning keepalive off doesn't unload them, and updating an app like 7-Zip may hit "file in use" until Explorer restarts. Maybe hold a reference with GetModuleHandleEx without the pin flag, and free it when the setting is turned off?
  • Target branch. Since it's a new feature, it will go into the 2.4.x alpha first, not straight into the stable branch.

None of this is urgent. I'll know more after testing it on Sunday.

@Abdullah-Masood-05 Abdullah-Masood-05 self-assigned this Nov 25, 2026
Most of the wait before the menu shows is Windows loading the handlers other apps add to it (7-Zip, Windows Terminal, WinMerge and so on). The first right-click after Explorer starts loads all of them, and handlers that run in their own process shut down soon after a menu closes, so the next right-click after a pause has to start them again.

Two new settings, both off unless set; the app's Behavior page has a switch for each:

- preload: shortly after Explorer starts, build Windows' menus for a file, a folder and the backgrounds in the background, without showing them. A preload that never finished (Explorer crashed or was closed during it) keeps later ones from running until the config changes, so a handler that crashes Explorer there doesn't do so at every start.
- keepalive: keep a reference on handler DLLs and keep out-of-process handlers running between menus, and create those from the class factory each menu thread kept instead of asking RPCSS again. Turning it off lets all of it go at the next menu.
@alchemyyy

Copy link
Copy Markdown
Author

Thanks so much for the review. I did leave it a bit dirty. I've gone ahead and made the following changes:

  • Default on: the shipped shell.nss no longer turns either setting on. I squashed the branch, so it doesn't touch shell.nss (or the shipped-hash list) at all. Both are off unless turned on from the Behavior page or by hand, and the Behavior docs now list them as Off.

  • Crash at startup could repeat: done, with a marker in HKCU\Software\Nilesoft\Shell\Preload:

    • Before it builds any menus, the preload writes a value named after its Explorer process. The value holds the process's start time and a fingerprint of the config files. The preload deletes it when it's done.
    • If a marker is there and its Explorer is gone, the last run never finished. The preload is skipped and shell.log says why.
    • A marker from an Explorer that's still running doesn't count, so separate folder-window processes don't block each other.
    • One difference from your suggestion: it keeps skipping until the config changes, not just once. Skipping once would still crash on every other start if one app's handler always crashes there. Any saved config change re-arms it, for example turning preload off and on again. The downside is that closing Explorer while the preload is running also turns it off until the next config change. It's easy to switch to skip-once if you'd rather.
  • Pinned DLLs stay loaded: Shell now holds a plain GetModuleHandleEx reference, one per DLL, without the pin flag. Once keepalive is turned off, the next right-click releases those references (FreeLibrary, on the keepalive thread), along with the out-of-process servers it held. Windows then unloads them as it does without keepalive. The release happens at the next right-click rather than the moment you save, because that's when Shell reloads the config.

  • Target branch: I don't see a 2.4.x branch yet (dev looks stale). Once you create one, I'll rebase onto it and change the PR's base.

I'm running the new build in Explorer now, and the preload runs normally.

Some apps add menu items only for certain file types (photo viewers, Office, media players), so the menus the preload builds at Explorer start (a file, a folder and the backgrounds) don't load them, and the first right-click on such a file still waits for them.

- settings.preload.types: the extensions whose menus the preload builds too, from an empty file of each kept in a folder of the temp folder. A list (['jpg', 'pdf']) or one string ('jpg|pdf'), with or without dots; unset, jpg, png, pdf, docx, mp4, mkv, html and obj; '' builds none. shell.log names the types it built.
- preload is a block now, like tip: preload = true still works, as preload.enabled.
- The app's Behavior page lists the file types under the preload switch, with an Add box, a remove button on each and a Reset button at the right of the list's header.

In Explorer the preload built the eight default types in 750 ms at start, and the photo viewer, MP4, MKV and Cast to Device handlers were loaded before the first right-click.
@Abdullah-Masood-05

Copy link
Copy Markdown
Owner

Thanks for the quick turnaround, @alchemyyy. I went through the new commits and all three fixes check out:

  • Both settings are off by default, and shell.nss and the shipped-hash list are untouched.
  • The crash marker handles PID reuse through the process start time, and skipping until the config changes makes sense to me. Keep it that way.
  • The plain GetModuleHandleEx references balance out, and FreeLibrary runs once per DLL.

Two smaller things, if you have time before Friday:

  1. Reading the config while it's being replaced. with_settings in Warm.cpp uses Initializer::instance->cache without a lock, and Initializer.cpp deletes it in uninit() when the config reloads. If someone saves shell.nss while the preload or Created() is reading the settings, Explorer could read freed memory. The existing CoCreateInstance hook has the same exposure, so I won't block on it, but taking the initializer's lock or copying the few values you need would close it for the new threads.
  2. Sample files. The %TEMP%\Nilesoft Shell preload\sample.<ext> files are left behind after the preload. Deleting them when it finishes would be tidier.

About the branch: no need to rebase. Targeting main is fine.

The plan: I'll do my best to release a beta with this PR and #20 merged on Friday night. If nobody reports bugs by Sunday, the new stable goes out Sunday night. So please hold off on bigger changes until Friday night; small fixes like the two above are welcome before then.

@Abdullah-Masood-05
Abdullah-Masood-05 changed the base branch from main to prs/2.3.2 November 27, 2026 13:52
@Abdullah-Masood-05
Abdullah-Masood-05 merged commit 083a623 into Abdullah-Masood-05:prs/2.3.2 Nov 27, 2026
6 checks passed
@Abdullah-Masood-05

Copy link
Copy Markdown
Owner

Merged, thank you @alchemyyy! This is a great contribution, and the quick turnaround on the review points made it easy to take in.

It's in the 2.3.2 beta (v2.3.2-beta.1), which is building now and should be out in about 20 minutes. Both settings are off by default, with their switches on the Behavior page. If nothing turns up during the beta, it goes into the stable release on Sunday night.

The two small follow-ups from my last comment (the lock around the settings and cleaning up the sample files) are still welcome as a new PR whenever you have time.

@alchemyyy

Copy link
Copy Markdown
Author

Merged, thank you @alchemyyy! This is a great contribution, and the quick turnaround on the review points made it easy to take in.

It's in the 2.3.2 beta (v2.3.2-beta.1), which is building now and should be out in about 20 minutes. Both settings are off by default, with their switches on the Behavior page. If nothing turns up during the beta, it goes into the stable release on Sunday night.

The two small follow-ups from my last comment (the lock around the settings and cleaning up the sample files) are still welcome as a new PR whenever you have time.

Howdy! Thanks so much for the merge, and I'm glad you like the feature too. Apologies for missing the deadline on those two follow-up items. I try to avoid messing with code I'm going to put in a PR when I don't have a time stretch to really sit down with it.

The follow-up PR is here: #22

Side note (just more performance related junk) - I did some additional digging around yesterday and found that there's a speckling of shell extensions which can contribute relatively significantly to context menu load times, and in many cases at least for me, are never used. It might a fun feature in the future to expand on the existing rule set remove feature with this in mind.

@vahid-code

Copy link
Copy Markdown

Are you sure this feature works as expected? I tried v2.3.2 beta 3 and enabled both options but first right click after windows restart still takes longer. both on empty desktop space and on a file type that is set to preload. also waited 10s after windows boot before making the first right click to give time to preloading. but still first time takes longer.

@Abdullah-Masood-05

Copy link
Copy Markdown
Owner

@vahid-code Thanks for testing it. The preload should be done well within 10 seconds, so something is getting in the way on your PC. Could you send me a few things?

  1. The lines that start with Preload: in %LOCALAPPDATA%\Nilesoft\Shell\shell.log, from a fresh restart. There should be one saying the menus were built in some number of ms, or a warning saying why it was skipped. If there is no Preload: line at all, that tells me something too.
  2. The output of this command in PowerShell:
    (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers').PSChildName
    Windows only loads the first 15 of these at startup. If Shell is past that point, it doesn't load until your first right-click, so there's nothing to preload with.
  3. Roughly how long the first right-click takes compared with the second one. Does it change if you wait a full minute after logging in?

With these I can tell whether the preload ran and missed something, or never got a chance to run.

@vahid-code

vahid-code commented Nov 28, 2026 •

Copy link
Copy Markdown

@Abdullah-Masood-05

First of all I think I have found the problem. after testing the number 2, I noticed there was no nilesoft shell entry in that registry path. then checked my main pc and noticed that there is a nilesoft shell entry there. so I re registered nilesoft shell in the test system using the method I used in my main pc: removed NilesoftShellPro.exe file and ran the shell.exe that shows the original small window with 3 options to register or unregister and explorer restart when settings app is removed. registered the shell that way and the registry entry added and preload worked fine. then because I was testing in virtual machine I revetred to the snapshot before installing the nilesoft shell and did the installing one more time to make sure I did it right and this time also there was no entry added for nilesoft shell in that registry path when registered shell using settings app. in both tests I ran the settings app and shell.exe as administrator. also note that I used portable version.

By the way this is the answer to your 3 questions (I wrote these before the top explanation):

1- The shell.log has the lines starting with preload. the last 3 of them is:

2026-11-28 02:56:29 [info] Preload: Windows' menus for a file, a folder, the backgrounds and these file types built in 1297 ms: jpg, png, mp4, mkv, html, zip, rar, php.
2026-11-28 02:57:37 [info] Preload: Windows' menus for a file, a folder, the backgrounds and these file types built in 1547 ms: jpg, png, mp4, mkv, html, zip, rar, php.
2026-11-28 11:08:15 [info] Preload: Windows' menus for a file, a folder, the backgrounds and these file types built in 1250 ms: jpg, png, mp4, mkv, html, zip, rar, php.

2- The output of powershell command:


IDM Shell Extension
OneDrive1
OneDrive2
OneDrive3
OneDrive4
OneDrive5
OneDrive6
OneDrive7
EnhancedStorageShell
Offline Files

3- The first right click takes more than 3 times the second one. also changes the mouse cursor to loading cursor while loading but the second right click does not change mouse cursor. I also waited more than 5 minutes and it was the same behavior.

@Abdullah-Masood-05

Copy link
Copy Markdown
Owner

@vahid-code Thank you, that was a great find, and sorry for the slow reply.

You were right. When Shell is registered from the settings app with Register for you, it registered only the menu itself. The other way of registering also adds an entry that makes File Explorer load Shell as soon as it starts. Without that entry, Shell only loaded on the first right-click, so the preload couldn't help that click. Your shell.log shows the preload did run, just too late.

The fix is ready: Register for you now adds that entry too, still without needing administrator rights. It isn't in beta 5, which was already building, but it will be in 2.3.2 stable, which I'm releasing tomorrow. Until then, registering with shell.exe's own window, or with Register for all users in the app, works.

Beta 5 is here if you'd like to check the rest before the release: https://github.com/Abdullah-Masood-05/Shell/releases/tag/v2.3.2-beta.5. If you find any other bug, please let me know so I can look into it before then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants