Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:15:39+00:00 2026-06-06T01:15:39+00:00

I’m trying to update a piece of code so that it simulates modal dialogs

  • 0

I’m trying to update a piece of code so that it simulates modal dialogs and Reactive Extensions felt like a proper tool to do so, but I can’t make it work.

Currently, the code looks something like this:

public bool ShowConfirmationDialogs(IEnumerable<ItemType> items)
{
    bool canContinue = true;

    foreach (var item in items)
    {
        Dialog dialog = new Dialog();
        dialog.Prepare(item);   // Prepares a "dialog" specific to each item

        IObservable<bool> o = Service.ShowDialog(dialog, result =>
        {
             // do stuff with result that may impact next iteration, e.g.
             canContinue = !result.Condition;
        });

        // tried using the following line to wait for observable to complete
        // but it blocks the UI thread
        o.FirstOrDefault();

        if (!canContinue)
            break;
    }

    if (!canContinue)
    {
        // do something that changes current object's state
    }

    return canContinue;
}

Up until now, the code from the lambda expression was used to do stuff when the “dialog” shown by ShowDialog was closed. The call to ShowDialog is non blocking and used to return void.

What happens behind the scenes in ShowDialog is that an object is added to an ObservableCollection so that it is shown on the screen.

I modified ShowDialog so that it will return an IObservable<bool> who calls OnCompleted of its subscribers when the dialog is closed. This works. I tested it with the following code:

o.Subscribe(b => Console.WriteLine(b), () => Console.WriteLine("Completed"));

And I can see the string "Completed" when the dialog is closed. My problem is that the line above is non-blocking, so I can potentially display several dialogs, which I don’t want to do.

I tried the following:

o.FirstOrDefault();

assuming that the program would wait there until the observable sent something or completed. The program blocks all right, but it also freezes the UI, which means I never see my dialog, which I can never close, so the observable never completes.

I tried several variations using ObserveOn and SubscribeOn to try to leave the UI thread do its work, with no luck. Any ideas would be greatly appreciated, my main goal being to keep the code looking sequential, kind of like when using Window.ShowDialog.

To summarize: (and to answer Chris in the comments)

The problem is that ShowDialog is non-blocking and, as stated above, the expected behavior is the same as when using Window.ShowDialog. Right now, I can either not block—but then the loop continues and I get several dialogs—or I can block (with FirstOrDefault), but it also blocks the UI, which prevents me from closing the dialog in order to complete the observable.

More explanations: (for Enigmativity)

When I call ShowDialog a control is displayed that is modal—in the sense that it blocks the user from accessing the rest of the application—but the call to the method is non-blocking, so execution continues immediately. In my example, this can potentially display several dialogs because of the loop. The method is non-blocking because all it does is add an object to a collection and I can’t change this behaviour.

However, hoping to use Rx, I made it so that ShowDialog will return an IObservable. So now the method returns immediately, but I have an object that will call OnCompleted of any observers once the control that was displayed by ShowDialog‘s actions is closed. I’m using a Subject for this, in case it matters.

What I want now, is to wait for this returned IObservable to complete before moving on, and so simulate a blocking call. FirstOrDefault does the waiting part successfully, but unfortunately, it also blocks the UI thread, preventing the control from actually showing, thus preventing the user from closing it, thus preventing the IObservable from completing.

I know my idea can’t be far off, because I can get things to kind of work by closing the dialog automatically after x seconds. All I need now is for the “waiting” part not to block the UI so that the user can close the control instead of a timer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:15:41+00:00Added an answer on June 6, 2026 at 1:15 am

    I found a solution to my problem, so I’ll share it in case you’re interested.

    After some refactoring, I renamed the service’s method I used in the question and created a new one. It’s interface looks like this:

    public interface IDialogService
    {
        /// <summary>
        /// Displays the specified dialog.
        /// </summary>
        /// <remarks>This method is non-blocking. If you need to access the return value of the dialog, you can either
        /// provide a callback method or subscribe to the <see cref="T:System.IObservable{bool?}" /> that is returned.</remarks>
        /// <param name="dialog">The dialog to display.</param>
        /// <param name="callback">The callback to be called when the dialog closes.</param>
        /// <returns>An <see cref="T:System.IObservable{bool?}" /> that broadcasts the value returned by the dialog
        /// to any observers.</returns>
        IObservable<bool?> Show(Dialog dialog, Action<bool?> callback = null);
    
        /// <summary>
        /// Displays the specified dialog. This method waits for the dialog to close before it returns.
        /// </summary>
        /// <remarks>This method waits for the dialog to close before it returns. If you need to show a dialog and
        /// return immediately, use <see cref="M:Show"/>.</remarks>
        /// <param name="dialog">The dialog to display.</param>
        /// <returns>The value returned by the dialog.</returns>
        bool? ShowDialog(Dialog dialog);
    }
    

    The part that solves my problem is the implementation of ShowDialog:

    public bool? ShowDialog(Dialog dialog)
    {
        // This will hold the result returned by the dialog
        bool? result = null;
    
        // We show a dialog using the method that returns an IObservable
        var subject = this.Show(dialog);
    
        // but we have to wait for it to close on another thread, otherwise we'll block the UI
        // we do this by preparing  a new DispatcherFrame that exits when we get a value
        // back from the dialog
        DispatcherFrame frame = new DispatcherFrame();
    
        // So start observing on a new thread. The Start method will return immediately.
        new Thread((ThreadStart)(() =>
        {
            // This line will block on the new thread until the subject sends an OnNext or an OnComplete
            result = subject.FirstOrDefault();
    
            // once we get the result from the dialog, we can tell the frame to stop
            frame.Continue = false;
        })).Start();
    
        // This gets executed immediately after Thread.Start
        // The Dispatcher will now wait for the frame to stop before continuing
        // but since we are not blocking the current frame, the UI is still responsive
        Dispatcher.PushFrame(frame);
    
        return result;
    }
    

    I think the comments should be enough to understand the code, which I can now use as so:

    public bool? ShowConfirmationDialogs(IEnumerable<ItemType> items)
    {
        bool canContinue = true;
    
        foreach (var item in items)
        {
            Dialog dialog = new Dialog();
            dialog.Prepare(item);   // Prepares a "dialog" specific to each item
    
            bool? result = Service.ShowDialog(dialog);
    
            canContinue = result.HasValue && result.Value;
    
            if (!canContinue)
                break;
        }
    
        if (!canContinue)
        {
            // do something that changes current object's state
        }
    
        return canContinue;
    }
    

    I’d love to hear any comments or alternatives other users may have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.