简体   繁体   中英

Background worker and progress bar not working properly

Hi was trying to have a progress bar to load for my encryption and compression application . I'm trying to use the background worker to update the progress bar on the time taken for the compression and encryption processed but somehow the application show the encrytion fail msg box that i have included inside the button rather than a success.

This is the code for my button

    private void lockButton_Click(object sender, EventArgs e)
    {

        if (this.passwordtextBox.Text == "")
        {
            MessageBox.Show("Please enter a password!");
        }
        else if (this.retypeTextBox.Text == "")
        {
            MessageBox.Show("Please retype password!");
        }
        else if (this.passwordtextBox.Text == this.retypeTextBox.Text)
        {
            details = new Details();
            details.SetPassword(this.passwordtextBox.Text);

            if (this.EncryptionComboBox.Text == "AES")
            {
                details.SetEncryption(this.EncryptionComboBox.Text);

                if (details.GetResult() == true)
                {
                    // Start the background worker
                    backgroundWorker1.RunWorkerAsync();
                }

                if (details.GetResult() == true)
                {
                    MessageBox.Show("Lock Success!");
                }
                else
                {
                    MessageBox.Show("Lock Unsuccess! Please try again");
                }
            }
        }
        else
        {
            MessageBox.Show("The password and verified password does not match!");
        }
    }

And this is my background worker code

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        //declare lockcontroller to be used
        LockController lControl = new LockController();

        //run zipfile method and store result to fName
        lControl.compress(ifile, details);
        lControl.Encrypt(details);
        lControl.LockCleaner(details);
        int i = 100;

        // Report progress to 'UI' thread
        backgroundWorker1.ReportProgress(i);

        // Simulate long task
        System.Threading.Thread.Sleep(0000);            
    }

I was wondering where it went wrong. progress bar and both encryption not working..

  1. Use string.IsNullOrEmpty(string) instead of something == ""
  2. You have no Worker Progress Event setup
  3. It appears you added your background worker via the UI Designer - create these in code - much cleaner
  4. kmcc049 is right - I don't see a completed handler either
  5. Check this link for more info on how to do this. http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

when you execute this line backgroundWorker1.RunWorkerAsync(); it immediately returns and executed the next line. You need to subscribe to the RunWorkerCompleted event for your message box.

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