D
D
danij_com2017-11-20 20:57:20
C++ / C#
danij_com, 2017-11-20 20:57:20

How to terminate a winforms application?

Hello.
I have a c# winforms application. All the necessary code is executed by functions even before the form itself is loaded (in theory), it looks like this:

public Form1()
{
            this.Opacity = 0;
            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            doit();
}

Actually, in the doit function, I need to close this application. Due to the fact that the form should not be loaded (I even removed Form1_Load from the code), the closing options do not work adequately (which I just did not try). All the time it crashes "Application *appname* has completed its work."
I need an option, how can I terminate the application from the same function, without opening the completion window.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2017-11-20
@Griboks

Remove all code associated with the form. Make the initialization method static. Specify the type "console application" in the project settings, and the initialization method as the entry point.

I
Ivan Arxont, 2017-11-21
@arxont

If Application.Exit crashes, then look at the FormClosing event. Apparently something is being done in it, but in general, run it in debug mode and show the full error.
PS: As a crutch, you can use Environment.Exit(0);

public Form1()
        {
            InitializeComponent();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question