Windows Form freezing on show()

1.2k Views Asked by At

Having a problem with my windows form. It's a very basic windows form, called from a seperate class. It has 1 button and 1 textfield. It takes 1 argument of a client

var form = new Form1(client);

form.Show();

Form1 code:

public partial class Form1 : Form
{
    private DiscordClient client;

    public Form1(DiscordClient client)
    {
        this.client = client;

        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var usernameToKick = username.Text;

        var userToKick = client.Servers.FirstOrDefault().Users.Where(input => input.Name == usernameToKick).FirstOrDefault();

        userToKick.Kick();
    }

    private void username_TextChanged(object sender, EventArgs e)
    {

    }
}

This freezes my form for about 30 seconds, before the form shuts down again. Why does this happen?

1

There are 1 best solutions below

1
On BEST ANSWER
Form form;


form = new Form1(client, e);

            var thread = new Thread(OpenAdminPanel);

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();


private void OpenAdminPanel()
    {
        Application.Run(AdminPanel);
    }