进度条在c#中不起作用

时间:2017-05-21 16:37:21

标签: progress-bar backgroundworker

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        this.progressBar2.Value = e.ProgressPercentage;
        //progressBar2.Value = e.ProgressPercentage;
    }
    private void button3_Click(object sender, EventArgs e)
    {
        backgroundWorker1.CancelAsync();
    }
    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        if (e.Cancelled)
        {
            textBox1.Text = "cancelled";
        }
        else if (e.Error != null)
        {

            throw e.Error;
        }
        else
        {
            textBox1.Text = string.Format("{0} files updated", e.Result);
        }
    }
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {

        if (backgroundWorker1.CancellationPending)
        {
            e.Cancel = true;
            return;
        }
        else
        {
            try
            {
                string password = @"myKey123"; // Your Key Here
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                string cryptFile = EncryOutputFile;

                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateEncryptor(key, key),
                    CryptoStreamMode.Write);

                FileStream fsIn = new FileStream(EncryInputFile, FileMode.Open);

                int data;
                double counter = 1;
                while ((data = fsIn.ReadByte()) != -1)
                {
                    cs.WriteByte((byte)data);
                    backgroundWorker1.ReportProgress((int)((counter / fsIn.Length) * 100));
                    counter++;
                }

                fsIn.Close();
                cs.Close();
                fsCrypt.Close();
            }
            catch
            {

            }
        }
    }

进度条不起作用。 backgroundWorker1.ReportProgress((int)((counter / fsIn.Length)* 100)); 添加这句话,它将无法正常工作...... 并且..加入比之前更多的加密非常慢。 :(

0 个答案:

没有答案