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 546323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:51:18+00:00 2026-05-13T10:51:18+00:00

How can I force ShowDialog to return from a background thread? I have a

  • 0

How can I force ShowDialog to return from a background thread?

I have a WPF application built on top of a C++/CLR framework which is listening to messages sent by the framework. One particular message is so important that I need to close all the current windows. However, if a modal Dialog (created by ShowDialog from my main window) is active and awaiting user input, the window will not close because it is waiting for ShowDialog to return. How can I force the modal Dialog to close and unwind code execution?

I tried setting the DialogResult or calling Close however that doesn’t seem to work.

Edit: The Dialog is created by my main window which expects a return value, like so:
(inside a click event handler in MainWindow):

Window modalDialog = new Window();
bool ret = (bool)modalDialog.ShowDialog();
if (ret == true)
{
 // do stuff
}
else
{
 // do some other stuff
}

When the framework sends a message (coming in on a different thread than the UI thread), I call MainWindow.Close(). The modal dialog gets closed at this point, however the code checking the return value of the Dialog (after ShowDialog) is still on the stack. Somehow this forces the main window to not disappear.

  • 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-05-13T10:51:18+00:00Added an answer on May 13, 2026 at 10:51 am

    When the dialog receives the WM_CLOSE message it sets a flag that causes ShowDialog to return after the WM_CLOSE and any containing messages have been fully processed. So if you are doing a Dispatcher.Invoke() onto your thread to call MainWindow.Close(), the ShowDialog() won’t return until after the Dispatcher.Invoke() has completed.

    So the simple solution is to return from the Dispatcher.Invoke() to get the ShowDialog() to return. If more processing is required, do another Dispatcher.Invoke() immediately afterwards.

    If you are doing the thread transition some other way than Dispatcher.Invoke(), or if the ShowDialog() doesn’t return after the Dispatcher.Invoke() call returns, you may have to post some of the code surrounding your Window.Close() call so we can see what you are actually doing.

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

Sidebar

Related Questions

I have a WPF application which calls OpenFileDialog.ShowDialog. While this dialog is open it
Okay, I've got my normal app which is in portrait mode. I can force
Is there any way I can force WPF rendering tiers mentioned here? http://msdn.microsoft.com/en-us/library/ms742196.aspx For
I have read a lot of posts about how you can force lightbox (or
I'm trying to find out how I can force elements to start from the
I have a site running on an Azure web role and I can force
In my WPF application I have a lot of custom dialog boxes that pop
Is there a way I can force a UDP socket ( which is used
From Beej's Guide to Network programming You can force it to use IPv4 or
Recently switched from MacVim to standard vim so I can force myself to move

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.