Coolguy
Coolguy

Reputation: 2285

C# Winform freeze when using background worker

Below is my coding:

Form2 msgForm;
private void button3_Click_1(object sender, EventArgs e)
{

    bw.WorkerReportsProgress = true;
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    //bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

    msgForm = new Form2();

    try
    {
        bw.RunWorkerAsync(comboBox15.Text);

        msgForm.ShowDialog();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    string PrtAdd = e.Argument.ToString();

    uploadlogo(PrtAdd);
   // uploadlogo(PrtAdd) is the coding that transmit serial protocol and will last around 2 minutes to finish.        
}

void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    msgForm.Close();
}

My coding is for running a backgroundworker when I click the button because clicking the button will transmit the protocols and it takes around 2 minutes.

During this 2 minutes, it will show "Please wait" in form2.

The problem is when I run this coding and click the button to run the background worker, my winform UI freeze. Is there a way not to freeze the UI?

Upvotes: 0

Views: 500

Answers (1)

Random Dev
Random Dev

Reputation: 52290

does it freeze because you show a dialog (msgForm.ShowDialog)?

I do think so - try only Show

Upvotes: 2

Related Questions