bwells Posted January 26, 2004 Posted January 26, 2004 I have a form which is my main application. I want my app form to be up on the screen, and then I want a modal form to be on the screen and the user must select an option from this modal form before they can work in the main app. In order to do this, I ran the modal form on its own thread because if I do not, then the modal form blocks the main form and I do not see the main form until after the user has selected their option from the modal form. As a result of my modal form being in its own thread, I am unable to start a timer in response to the user's selection on the modal form because the thread the modal form is running in ends after the user makes their selection. Fruthermore, after the user selects something from the modal form, I want to add a component to the collection of controls owned by the main form. But if I try and add to this collection from the modal form thread, I get an exception saying that I cannot add to the collection from a separate thread. So my questions are: 1) How can I show the user a modal form on top of the main app and now block the main app? Is there a way to do this without putting my modal form on a separate thread? 2) If my modal form must be on a separate thread, then how can I start a timer from the main thread with an even that happens on a separate, temporary thread? Is there a way I can cause an event to happen on the main thread from a separate temporary thread? 3) Is there a way I can add controls to my main app form from a temporary thread? The basic deal seems to be if I can find a way that my temporary thread can tell my main form to do things on the main thread, that would fix things. The other option would be to find a way to get my main form up on tthe screen and have a modal form show on top of that, withouth running my modal form in a spearate thread to prevent it from blocking the loading of the main form. But the problem with this is I still have to add to the main form's collection of conrols which must be done on the main thread. thanks Bryan Quote
jmpirelli Posted January 27, 2004 Posted January 27, 2004 1) I'm not sure I understand what you mean. You'd like to force the user to enter some information, which calls for a modal dialog. So far, so good. What do you mean by "the modal form blocks the main form" ? Since you're waiting for input, it looks like this is normal behaviour. By going into a 2nd thread, you've actually created a modeless dialog. It would be simpler to call f.Show() instead of f.ShowDialog() (assuming f is your input form). 2. You cannot control in which thread a timer function is called (it might even be a different thread than the one that created the timer). WinForms has a mechanism to handle this situation. Basically, you call a function fn on your form (or any control) from any thread. From fn, you check if you're on the right thread (through the InvokeRequired property). If not, you ask WinForms to re-call fn from the right thread (through the Invoke method). See an in-depth explanation by Chris Sells at http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.asp 3) Use the same mechanism as in 2) HTH, jmp Quote
bwells Posted January 28, 2004 Author Posted January 28, 2004 I see your point. The idea was that I want the main form to be up but blank until the user finishes dealing with the modal dialog. Then I want to populate the application form with controls based on what they selected from the "select project" dialog. So the idea is that I want the main app to be fully built and displayed, and then I want to pop up a modal dialog that they have to deal with. The main app being in the background is to look good, and actually has no added value. But I dont want this modal dialog to be made visible in response to anything the user does. Instread I want it to be visible AFTER the main form is fully done and visible. But if I start the modal dialog from the OnLoad event, then it blocks the main app form from showing. Then, the thread issue with the timer is because of what happened. If I start the timer from the thread I run the modal dialog from, then the timer does not run. It ends when the modal dialog thread ends. So after the user is done with the modal dialog, I want to populate the main app with controls and then I want to start the timer. So perhaps I can start the timer when the child form of the main app is visible. But I still do not know how to show the modal dialog on top of the main app form before the user does anything. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.