Async and cancellable dialogs - #2886
Draft
Descolada wants to merge 1 commit into
Draft
Conversation
Descolada
marked this pull request as draft
June 14, 2026 17:16
Descolada
force-pushed
the
MessgeBoxShowAsync
branch
from
June 14, 2026 19:39
c253910 to
d469b2b
Compare
Descolada
force-pushed
the
MessgeBoxShowAsync
branch
from
June 14, 2026 19:47
820aa4d to
e60074a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #838 — adds an asynchronous, cancellable way to show message boxes, common dialogs, and modal dialogs across platforms.
This started as just needing async/cancellable
MessageBox.Showon GTK, but grew into a fairly complete implementation covering message boxes, the common dialogs (color/font/file/folder), and customDialogs.API
MessageBox.ShowAsync(...)— async overloads mirroringMessageBox.Show, each taking an optionalCancellationToken.CommonDialog.ShowDialogAsync(parent, CancellationToken)— coversOpenFileDialog,SaveFileDialog,ColorDialog,FontDialog,SelectFolderDialog,AboutDialog, etc.Dialog.ShowModalAsync(CancellationToken)/Dialog<T>.ShowModalAsync(CancellationToken).Semantics:
NotSupportedExceptionis thrown.Dialog<T>.Result) completes the task normally; the task only faults withOperationCanceledExceptionwhen the token closed it.Shared orchestration lives in
AsyncModalDialog: it posts the blockingShowDialogonto the UI thread (where the native modal loop pumps) and invokes the handler's cancel callback — while it's showing — to break that loop when the token is signalled.Windows: WH_CBT hook
The original draft of this PR used a hidden dummy owner form because there was no reliable way to identify the native dialog to dismiss it. That's replaced with a cleaner approach:
Just before the blocking
ShowDialogcall, a thread-localWH_CBThook is installed (Win32.ModalDialogHook). It captures the dialog's window handle on firstHCBT_ACTIVATE, then unhooks itself. Cancellation postsWM_CLOSEto that exact handle — so the right dialog is dismissed unambiguously even when several are open, with no host/owner window and no flicker. Handlers compose this via the smallWin32.CancellableModalDialoghelper.This covers, on both WinForms and WPF:
MessageBox,ColorDialog,FontDialog,OpenFileDialog/SaveFileDialog, andSelectFolderDialog.Managed/themed dialogs that aren't native windows (
ThemedAboutDialogHandler, and on WPF after the Xceed removal,ThemedColorDialogHandler/ThemedFontDialogHandler) are made cancellable by simply closing the underlying EtoDialog— no native interop needed.Platform status
ShowDialogAsyncon these should be considered a starting point, not finished.Not supported
OpenWithDialogon Windows remains a fire-and-forget shell launch (rundll32 OpenAs_RunDLL), so it doesn't support cancellation. (SHOpenWithDialogwas evaluated but on Windows 11 it's a UWP picker that can't distinguish select from cancel, although it does block until the dialog closes.) The async API surfaces this asNotSupportedException, which the test harness catches and logs.