question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question on how to show a modal dialog synchronously

See original GitHub issue

In WPF, the Window.ShowDialog(); method blocks the thread and continues when the dialog is closed (the same way MessageBox.Show(); works in WinForms). In Avalonia, the docs indicate that Window.ShowDialog() executes asynchronously, meaning the thread can continue before the dialog is closed.

Maybe this is a matter of me not being good at async vs sync coding but how would I produce a similar result to WPF/WinForms but using the way Avalonia has been architected? I want to call ShowDialog from a non-async method and have execution pause until the dialog is closed.

I’ve tried the following:

Task.WhenAll(dialog.ShowDialog(parent));, dialog.ShowDialog(parent).Result;, and dialog.ShowDialog(parent).Wait(); block the dialog thread and prevents the dialog from loading.

Task.Run(() => ShowDialog()).GetAwaiter().GetResult();
private async Task ShowDialog()
{
    Dialog dialog = new Dialog();
    await dialog.ShowDialog(parent);
}

throws an exception of System.InvalidOperationException: ‘Call from invalid thread’

What is the proper way to do this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
sn4k3commented, Oct 6, 2020

@derekantrican if that works fine for you and you use it for multiple windows, consider making extension to ease your code. Something like:

public static void ShowDialogSync(this Window window, Window parent = null)
        {
            if (parent is null) parent = window;
            using (var source = new CancellationTokenSource())
            {
                window.ShowDialog(parent).ContinueWith(t => source.Cancel(), TaskScheduler.FromCurrentSynchronizationContext());
                Dispatcher.UIThread.MainLoop(source.Token);
            }
        }

        public static T ShowDialogSync<T>(this Window window, Window parent = null)
        {
            if (parent is null) parent = window;
            using (var source = new CancellationTokenSource())
            {
                var task = window.ShowDialog<T>(parent);
                task.ContinueWith(t => source.Cancel(), TaskScheduler.FromCurrentSynchronizationContext());
                Dispatcher.UIThread.MainLoop(source.Token);
                return task.Result;
            }

            return default(T);
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pure Javascript Modal does not show on synchronous ...
1. show the modal, then make the sync call behind a 1ms setTimeout, giving time for the dom to show the modal before...
Read more >
How to make confirm dialog synchronous
In reference to the confirm() method: I see you have a note on how to make the dialog modal, but it seems like...
Read more >
ShowDialog synchronously in UI for WPF | Telerik Forums
how I can display a modal window in XBAP synchronously so that the process does not continue until you close the window?
Read more >
How to Implement Modal Dialog Functions with Promise- ...
Learn how to implement modal dialog functions with promise-based dialog results in your Angular application. See more from Wijmo today.
Read more >
dialog
Displays a modal dialog that shows an error message. This API can be called safely before the ready event the app module emits,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found