简体   繁体   English

C#进度栏未使用BackgroundWorker更新

[英]C# progress bar not updating with backgroundworker

I am trying to create an Encryption/Decryption GUI and I have all the functionalities I want. 我正在尝试创建一个加密/解密GUI,并且具有我想要的所有功能。 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. 我在线搜索文章,他们都告诉我要使用backgroundWorker,以便进度条可以在单独的线程上运行,而不会冻结UI界面。 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. 什么也curSize改变while循环内的curSizetotSize的值,因此您将遇到无限循环。 The value of percentage is the same on every iteration, so your ProgressBar is never going to change value and will appear "stuck". percentage值在每次迭代中都是相同的,因此ProgressBar永远不会更改值,并且会显示为“卡住”。

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. 您需要弄清楚该循环内应该发生的情况,并修改curSize的值,以使百分比发生变化, ProgressBar移动,并且循环最终结束。

Maybe I'm missing something, but I don't see where you have bound the encryptBG_ProgressChanged method to the encryptBG event. 也许我缺少了一些东西,但是我看不到您将cryptoBG_ProgressChanged方法绑定到cryptoBG事件的位置。 Usually an event binding would be something like ".OnProgressChanged += encryptBG_ProgressChanged..." in your code 通常,事件绑定在代码中类似于“ .OnProgressChanged + = cryptoBG_ProgressChanged ...”。

That part of your code isn't visible here, so that could be a place to look. 您的那部分代码在这里不可见,因此可能是一个看起来的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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