I have a Form A that is displayed on a thread that also contains a heartbeat monitor. When an error occurs, we need the new form, Form B to be displayed until the close button is depressed and for Form A to be non-functional until that time; however, we need Form A code to not be blocked so the heartbeat will continue to be detected. Here is what has been attempted and the problems with each:
1. Originally, Form B was brought up with .Show and was non-modal. With this method, we used FormA.Enabled = false and then back to true. This operation allows the heartbeat thread to continue and the FormA to be inactive; however, it is very slow. .466 seconds to disableand .825 seconds to enable. The enabling is very evident on Form A to the user as they try to continue.

2. Making the Form B call modal worked well, except the Form A code was halted and the heartbeat thread died taking down the entire application.

3. I then tried to start a new thread to display Form B; however, the method to display the form as two arguments and is only supported with a callback delegate (we're in .Net 1.1 - so no generics). This seemed like a big change as we are going into system test tomorrow, so I passed on this. I'm still not even certain it would have behaved modally being on a different thread than Form A and then I'd be back at using .Enabled again.

4. I tried using a timer to call the display method and learned that modal windows won't behave as modal when started from a timer on a different thread.

I believe I need to go back to method #1, but I need to find a way to speed up the .Enabled property. I read that I can put all of Form A's controls into a list to speed up the .Enabled, but there were no details of how .Enabled would know to use such a list. Does anyone have any information about how to speed up form.Enabled?

Recommended Answers

All 5 Replies

Can you upload a project? I have no idea what you're talking about.

I am sorry, but I can not post the actual code since it is proprietary. The scenario really boils down to two simple questions: 1. Can any one explain the inner workings of the form.enabled property so that I can have it use a list to traverse through the hierarchy of its own controls it has to enable? I have read that if enabled uses a list, it is faster; but I have no idea how to actually implement this idea. 2. Can any one explain how to make a form modal when the showDialog is called from a timer instead of the parent form thread?

Success in either of these areas would fix this problem.

I am currently working on setting the parent form when calling ShowDialog to see if the Form B window then behaves modally.

1. Can any one explain the inner workings of the form.enabled property so that I can have it use a list to traverse through the hierarchy of its own controls it has to enable? I have read that if enabled uses a list, it is faster; but I have no idea how to actually implement this idea.

You can iterate through all of the form's controls when the Form's enabled property is changed in the EnabledChanged event:

private void Form1_EnabledChanged(object sender, EventArgs e)
        {
            bool bEnabled = this.Enabled;
            foreach (Control ctrl in this.Controls)
            {
                ctrl.Enabled = bEnabled;
            }
        }

2. Can any one explain how to make a form modal when the showDialog is called from a timer instead of the parent form thread?

If your thread has access to the parent form's handle, I think you can pass that into the ShowDialog(IWin32Window owner) to get that behavior.

Thank you for your help. Idecided to go with the

ShowDialog(this.Parent)

suggestion. This did not work until I made this call directly from the form class that modal form was being displayed over. I am not sure why I couldn't make this call from the base class. I'm guessing it had something to do with being on a different thread. Your suggestion above encouraged me to keep trying the call various ways, and I found the right combination. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.