简体   繁体   中英

C# progress bar not updating with backgroundworker

I am trying to create an Encryption/Decryption GUI and I have all the functionalities I want. The problem is that I'm trying to get the progress bar running while I am encrypting a file. I searched for articles online and they all told me to use backgroundWorker so the progress bar can run on a separate thread while not freezing up the UI interface. However, I can't seem to get the progress bar to show anything.

public partial class Form1 : Form
{
   static string key = "";
   cSecureData ed = new cSecureData(key);

   public Form1()
    {
        InitializeComponent();
        InitializeEncryptBG();
    }

    private void InitializeEncryptBG()
    {
        encryptBG.DoWork += new DoWorkEventHandler(encryptBG_DoWork);
        encryptBG.RunWorkerCompleted += new RunWorkerCompletedEventHandler(encryptBG_RunWorkerCompleted);
        encryptBG.ProgressChanged += new ProgressChangedEventHandler(encryptBG_ProgressChanged);
    }

   private void btnEncrypt_Click(object sender, EventArgs e)
   {
        this.btnEncrypt.Enabled = false;
        this.btnDecrypt.Enabled = false;
        /*if (Rijndael.Checked == true)
        {
            ed.EncryptFile(textBox1.Text, textBox2.Text, "1");
        }
        else if (TripleDES.Checked == true)
        {
            ed.EncryptFile(textBox1.Text, textBox2.Text, "2");
        }*/
        encryptBG.RunWorkerAsync();
   }

   private void encryptBG_DoWork(object sender, DoWorkEventArgs e)
   {
        if (Rijndael.Checked == true)
        {
            ed.EncryptFile(textBox1.Text, textBox2.Text, "1");
        }
        else if (TripleDES.Checked == true)
        {
            ed.EncryptFile(textBox1.Text, textBox2.Text, "2");
        }
        FileInfo fInfo1 = new FileInfo(textBox1.Text);
        FileInfo fInfo2 = new FileInfo(textBox2.Text);
        double totSize = fInfo1.Length;
        double curSize = fInfo2.Length;
        while (curSize <= totSize)
        {
            Thread.Sleep(100);
            double percentage = curSize / totSize * 100;
            encryptBG.ReportProgress((int)percentage);
        }
   }

   private void encryptBG_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
        progressBar.Value = e.ProgressPercentage;
   }

   private void encryptBG_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
   {
        btnEncrypt.Enabled = true;
        btnDecrypt.Enabled = true;
        MessageBox.Show("Done");
   }
}

My encryption method is not here because I'm referencing the method from another project I made.

The problem is here:

while (curSize <= totSize)
{
    Thread.Sleep(100);
    double percentage = curSize / totSize * 100;
    encryptBG.ReportProgress((int)percentage);
}

Nothing changes the value of curSize or totSize inside that while loop, so you've got an infinite loop. The value of percentage is the same on every iteration, so your ProgressBar is never going to change value and will appear "stuck".

You need to figure out what's supposed to happen inside that loop and modify the value of curSize such that the percentage changes, the ProgressBar moves, and the loop eventually ends.

Maybe I'm missing something, but I don't see where you have bound the encryptBG_ProgressChanged method to the encryptBG event. Usually an event binding would be something like ".OnProgressChanged += encryptBG_ProgressChanged..." in your code

That part of your code isn't visible here, so that could be a place to look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM