I created a simple autoclicker in C# VIsual Studio 2019 and when I open it and then close it with the red X in the top right it remains in background(I can see it in task manager). I tried to make a button with the code:
private void button2_Click(object sender, EventArgs e)
{
    Application.Exit();
}
but it still doesn't work. Any help?
Here is the full code:
public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern short GetAsyncKeyState(Keys vKey);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    private const int LEFTUP = 0x0004;
    private const int LEFTDOWN = 0x0002;
    public int intervals = 100;
    public bool Click = false;
    public int parsedValue;
    
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        Thread AC = new Thread(AutoClick);
        backgroundWorker1.RunWorkerAsync();
        AC.Start();
    }
    private void AutoClick()
    {
        while (true)
        {
            if(Click == true)
            {
                mouse_event(dwFlags: LEFTUP, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
                Thread.Sleep(1);
                mouse_event(dwFlags: LEFTDOWN, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
                Thread.Sleep(intervals);
            }
            Thread.Sleep(2);
        }
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            if (checkBox1.Checked)
            {
                if(GetAsyncKeyState(Keys.Down)< 0)
                {
                    Click = false;
                }
                else if(GetAsyncKeyState(Keys.Up)< 0)
                {
                    Click = true;
                }
                
                Thread.Sleep(1);
            }
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if(!int.TryParse(textBox1.Text, out parsedValue))
        {
            MessageBox.Show("Enter a number");
            return;
        }
        else
        {
            intervals = int.Parse(textBox1.Text);
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}