C# - How to Close a From and Application.Run

179 Views Asked by At

This simple code prevent application from closing

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Thread TH = new Thread(Run);
        TH.Start();
    }

    void Run()
    {
        Application.Run();
    }

On the other hand i can stop this with:

        Application.Exit();

But, this does not work with when [X] button on Form is pressed, the Debugging still working, and the console debug output gives me this, while Form is actually closed:

The thread 0x65c has exited with code 0 (0x0).
The thread 0x11c0 has exited with code 0 (0x0).

Any Idea how to actually stop this totally with [X] button?

1

There are 1 best solutions below

0
Treno On

Ok, I managed to fix it.

in Form1.Designer.cs inside InitializeComponent()

this needs to be added

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_Closing);

then in Form1.cs

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