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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:16:31+00:00 2026-05-20T09:16:31+00:00

For reasons I think are not relevant here I have one or more Threads

  • 0

For reasons I think are not relevant here I have one or more Threads that comunicate with a single instance UI (a Form).

In the worker threads I need to report progress or input data or simple choices. All of those come from user interaction with the UI and, of course, for M$ .NET, all the UI runs in the main thread.

Obviously I need to handle the thread synchronization across the UI (main) thread and the worker ones. I do it by properly verifying InvokeRequired and company.

There is a miriad of posts and articles around there discussing incoherences and subtleties in InvokeRequired, IsHandleCreated, IsDisposed and so on, so I won’t speak about it.

I just have to say that my UI, that is just a Form, was supposed to be shown as a modal or modeless form, depending on the caller wish.

One could just UI.Warn( "Warning!" ) while other could UI.Question( "Make a choice:", options... ).

Consider now the following excerpt from M$DN documentation:

Form.ShowDialog Method:

Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button…

I have never attended that a form shown as modal, nevertheless their implementors say it will not be destroyed, could fall in a non-usable state after closing (hiding).

But it does!

When the form returns from ShowDialog( ) its Handle is just trashed away in the belief that when it is needed again, ShowDialog( ) will be called and the handle will be recreated.

I have no idea why M$ stuff needed to do this way, but I just thought that I’d be able to have the very same form acting perfectly as modal or modeless without a problem. M$DN docs say nothing about it being forbidden (or I am too drunk to find it)!

Well, in the end it is a relativelly simple (and dirty) way to fix it.

var r = ShowDialog( );

// Handle thrown away aftr "ShowDialog()" supposing the
// next one will recreate it.
if ( !this.IsHandleCreated )
{
    // Force "Handle" recreation here, while in the main thread,
    // before any "Show()" happens.
    CreateHandle( );
}

return r;

It works, but I wonder if it shouldn’t be any decent way to reach the same end.
(Maybe prgramming in something that does not carry any limitting ancestor compatibility baggage as .NET still does…)

  • 1 1 Answer
  • 1 View
  • 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-20T09:16:32+00:00Added an answer on May 20, 2026 at 9:16 am

    Are you sure you don’t have this backward? I created a simple blank form to use as a modal dialog, and then tested it with a simple form that just has a button that shows the dialog.

    public partial class Form1 : Form
    {
        private MyDialog theDialog;
        public Form1()
        {
            InitializeComponent();
            theDialog = new MyDialog();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            theDialog.ShowDialog();
        }
    }
    

    I can show the dialog repeatedly with no trouble.

    Now, if I call theDialog.Show(), close it, and then try to show it again, I get a ObjectDisposedException.

    So, the documentation is correct: ShowDialog does not call Form.Close, whereas Show apparently does.

    EDIT:

    The documentation for Form.Close tells you what you have to do if you want to prevent the form from being destroyed:

    You can prevent the closing of a form
    at run time by handling the Closing
    event and setting the Cancel property
    of the CancelEventArgs passed as a
    parameter to your event handler.

    With that information and a few minutes’ thought, it’s trivial to have a form that you can show as modal or non-modal:

    public partial class Form1 : Form
    {
        private MyDialog theDialog;
        public Form1()
        {
            InitializeComponent();
            theDialog = new MyDialog();
            theDialog.FormClosing += new FormClosingEventHandler(theDialog_FormClosing);
        }
    
        void theDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            theDialog.Hide();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (theDialog.Visible)
            {
                theDialog.BringToFront();
            }
            else
            {
                theDialog.ShowDialog();
            }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            if (theDialog.Visible)
            {
                theDialog.BringToFront();
            }
            else
            {
                theDialog.Show();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's the question. Give only one reason you think why have OODB failed or
I think we may have trouble with our existing project. For some reasons we
for some reason (and I think I know why), when more than one row
Avoiding usual ORM for non-relevant reasons, I'm trying to write a class that can
I can think of plenty of good reasons to using it; however, what are
I can't think of many reasons (or any at all) in which multiple independent
I have not had much experience with VBA, but I sometimes use it at
I have an iPhone app problem that's been bugging me for a few days
I have a Windows WCF serivce and Web client. My service has one method
I have a tree view on the left side. Selecting a node displays relevant

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.