0

Someone asked quite a similar question here C# Application.Run without Form

but the checked answer doesn't suit me.

What I'd like to do is this :

static void Main()
{
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     ObjController objController = new ObjController();
     Application.Run();
     Application.Exit();
}

with

class ObjController
{

    Form1 form1 = new Form1();       

    public Form1 showObj()
    {
        form1.Show();
        return new Form1();        
    }
}

But when closing the form Application.Exit() in main() doesn't seem to execute. Why ? What can I do instead ?

Community
  • 1
  • 1
user310291
  • 36,946
  • 82
  • 271
  • 487
  • 2
    What do you mean "when closing the form Application.Exit() in main() doesn't seem to execute"? What does not execute? – Filip Vondrášek Feb 16 '13 at 19:27
  • 1
    Run will not return until exit is called. Is that what you are getting at? – Trevor Tubbs Feb 16 '13 at 19:33
  • 1
    Your concept or model of how things work is probably flawed. I'm the one who answered the question in the post you linked. Could you perhaps explain more clearly (1) What you want to do (2) What do really expect your posted source to do. – gideon Feb 16 '13 at 19:34
  • 1
    The code is pretty broken, that Exit() call isn't going to run until the Run() method returns. Which requires a call to Application.Exit(). The showObj() method is very seriously broken as well. What's intended is entirely unclear. Just don't do this until you've gained some insight in how Winforms works. – Hans Passant Feb 16 '13 at 19:50
  • @gideon I want that all each of my domain objects has a controller and everything command like showing an object form goes through that object controller, including the very first one. So I don't want the application to directly call the first form but call the controller of that form. – user310291 Feb 16 '13 at 19:56

1 Answers1

1

You will need to "listen" to the FormClosed event and call Application.Exit() there:

class ObjController
    {

        Form1 form1 = new Form1();


        public Form1 showObj()
        {
            form1.FormClosed += form1_FormClosed;

            form1.Show();
            return new Form1();

        }

        void form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
Mark PM
  • 2,909
  • 14
  • 19
  • `Application.Exit()` might not exit the application, if any other form responds to `FormClosing` by setting `e.Cancel`. But if that happens, the "main" form is already closed, and that is the way to exit the application, so the application continues to run forever. You should instead use `form1.FormClosing`, and pass its `FormClosingEventArgs` to [another overload of `Application.Exit()`](http://msdn.microsoft.com/en-us/library/ms223873%28v=vs.80%29.aspx) that lets you know whether the application should continue to run. –  Feb 16 '13 at 19:48
  • @hvd do you mean Mark should enhance his code ? If yes can you precise ? Thanks. – user310291 Feb 16 '13 at 20:02
  • @user310291 Yes, there are real valid situations that the code in this answer currently does not handle properly. The comment area is limited, so if can you be more specific about what part of my comment is not clear, I will try to explain further. –  Feb 16 '13 at 20:19
  • @hvd I am confused about formClosed and formClosing when I read msdn it's not clear either and I can't see what and how pass to "another overload of Application.Exit – user310291 Feb 16 '13 at 22:06
  • @user310291 Pressing the "X" close button first calls `FormClosing`, then closes the form, and finally calls `FormClosed`. If during the `FormClosed` event you realise the form should not have been closed, you're too late. `FormClosing` gives you a chance to signal that the "form close" operation should be cancelled by setting `e.Cancel`. `FormClosingEventArgs` derives from `CancelEventArgs`, so you can pass that directly. –  Feb 16 '13 at 22:40
  • @user310291 Say you have a "main" menu form, and you have a document form. Closing the main form should close all windows and exit the application, closing the document form first prompts "Save? Yes / No / Cancel". If the user attempts to close the main form, but then clicks "Cancel", the main form should remain open, or there won't be any way to exit the application at all. –  Feb 16 '13 at 22:41
  • I'm guessing you want showObj to return and instance of form1 instead of returning a new instance that isn't in use. – Trevor Tubbs Feb 17 '13 at 02:37