0

Class A has a Form1 (subclass of System.Windows.Forms.Form) member.

class A {
        Form1 form;
        public A()
        {
            form = new Form1();
            form.Show();
        }
    }
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        A a = new A();
        Application.Run();
    }

The problem is I do not know how to exit the program. I have tried Application.Exit() when handling the Form.Closed event or call A.Dispose(), but the Windows Task Manager still lists the process of my program.

How do I finish this program?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tatung
  • 23
  • 1
  • 8
  • try this link http://stackoverflow.com/questions/554408/why-would-application-exit-fail-to-work – Dinesh Apr 18 '12 at 09:16
  • @ta.speot.is `Application.Run` will not work as `a` is of type `A` and not `Form` or `ApplicationContext`. See [msdn](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.run.aspx) – gideon Apr 18 '12 at 09:17
  • @Gideon I read it too quickly. My mistake. – ta.speot.is Apr 18 '12 at 09:34
  • @ta.speot.is :) Yep that does happen especially when you have to read so fast on SO – gideon Apr 18 '12 at 09:37

4 Answers4

4

Application.Run has 3 overloads. You are using this one with no arguments.

Windows runs your program in a message loop, but it doesn't care about your form.
So if you close your form it doesn't matter; the program will still run.

The second overload is what everyone uses, Application.Run(Form). This one runs a Windows message loop over your form, so when you click close on the window, the application closes.

Your code should be:

class A {
    Form1 form;
    public A()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        form = new Form1();
        form.Show();
        Application.Run(form);
    }
}
[STAThread]
static void Main()
{
    A a = new A();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gideon
  • 19,329
  • 11
  • 72
  • 113
  • 1
    Nitpick: The constructor is responsible for too much. – ta.speot.is Apr 18 '12 at 10:13
  • 1
    @ta.speot.is somewhat true. ;) chiffree's answer is nice but then he would have to declare his **field** `form` as public to use outside class `A`. And then using a field like that is icky. The design is sort of upto the OP. – gideon Apr 18 '12 at 10:16
  • Maybe 'class A' should extend 'Form'? Than you could spare the "big" constructor and call 'Application.Run(a)'. – basti Apr 19 '12 at 10:24
2

Following Microsoft you should use this:

Application.Run(a.Form);

Because MSDN states that

Most Windows Forms developers will not need to use this version of the method. You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed.

basti
  • 2,649
  • 3
  • 31
  • 46
1

I think you have a mixup there. Check the documentation for Application.Exit.

There you will see that Exit will raise the Closed event for you, and calling Exit there might cause an infinite loop (which might be causing your problem, that the application is still visible).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dowhilefor
  • 10,971
  • 3
  • 28
  • 45
0

Try this:

Environment.Exit(1);
Zaki
  • 5,540
  • 7
  • 54
  • 91