Skip to content

Async and cancellable dialogs - #2886

Draft
Descolada wants to merge 1 commit into
picoe:developfrom
keysharp-org:MessgeBoxShowAsync
Draft

Async and cancellable dialogs#2886
Descolada wants to merge 1 commit into
picoe:developfrom
keysharp-org:MessgeBoxShowAsync

Conversation

@Descolada

@Descolada Descolada commented Jan 11, 2026

Copy link
Copy Markdown
Contributor

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.Show on GTK, but grew into a fairly complete implementation covering message boxes, the common dialogs (color/font/file/folder), and custom Dialogs.

API

  • MessageBox.ShowAsync(...) — async overloads mirroring MessageBox.Show, each taking an optional CancellationToken.
  • CommonDialog.ShowDialogAsync(parent, CancellationToken) — covers OpenFileDialog, SaveFileDialog, ColorDialog, FontDialog, SelectFolderDialog, AboutDialog, etc.
  • Dialog.ShowModalAsync(CancellationToken) / Dialog<T>.ShowModalAsync(CancellationToken).

Semantics:

  • With no token (or a non-cancellable one), the dialog is simply shown asynchronously on every platform — the native modal loop keeps the UI responsive while the returned task is awaited.
  • With a cancellable token, the platform handler must support cancellation; if it can't, a NotSupportedException is thrown.
  • An already-cancelled token short-circuits and never shows the dialog.
  • A user-initiated close (which may carry a Dialog<T>.Result) completes the task normally; the task only faults with OperationCanceledException when the token closed it.

Shared orchestration lives in AsyncModalDialog: it posts the blocking ShowDialog onto 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 ShowDialog call, a thread-local WH_CBT hook is installed (Win32.ModalDialogHook). It captures the dialog's window handle on first HCBT_ACTIVATE, then unhooks itself. Cancellation posts WM_CLOSE to 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 small Win32.CancellableModalDialog helper.

This covers, on both WinForms and WPF: MessageBox, ColorDialog, FontDialog, OpenFileDialog/SaveFileDialog, and SelectFolderDialog.

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 Eto Dialog — no native interop needed.

Platform status

  • GTK — manually tested, works as expected.
  • WinForms / WPF — manually tested, works as expected.
  • Mac / iOS / Android — preliminary and largely untested; ShowDialogAsync on these should be considered a starting point, not finished.

Not supported

  • OpenWithDialog on Windows remains a fire-and-forget shell launch (rundll32 OpenAs_RunDLL), so it doesn't support cancellation. (SHOpenWithDialog was 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 as NotSupportedException, which the test harness catches and logs.

@Descolada
Descolada marked this pull request as draft June 14, 2026 17:16
@Descolada
Descolada force-pushed the MessgeBoxShowAsync branch from c253910 to d469b2b Compare June 14, 2026 19:39
@Descolada
Descolada force-pushed the MessgeBoxShowAsync branch from 820aa4d to e60074a Compare June 14, 2026 19:47
@Descolada Descolada changed the title MessageBox.ShowAsync Async and cancellable dialogs Jun 14, 2026
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.

1 participant