后台工作者和进度条无法正常工作

发布于 2024-12-02 06:01:31 字数 2025 浏览 2 评论 0原文

您好,我试图为我的加密和压缩应用程序加载一个进度条。 我正在尝试使用后台工作人员来更新压缩和加密处理所需时间的进度条,但不知何故,应用程序显示我已包含在按钮内的加密失败消息框,而不是成功。

这是我的按钮的代码

    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!");
        }
    }

这是我的后台工作人员代码

    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);            
    }

我想知道哪里出了问题。进度条和加密都不起作用..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是一片海 2024-12-09 06:01:31
  1. 使用 string.IsNullOrEmpty(string) 而不是某些 == ""
  2. 你没有 Worker Progress Event 设置
  3. 看来你通过 UI Designer 添加了你的后台工作者 - 在代码中创建这些 - 更干净
  4. kmcc049 是对的 - 我没有看到完成的处理程序或者
  5. 检查此链接以获取有关如何执行此操作的更多信息。 http://msdn.microsoft.com/en-us/library /system.componentmodel.backgroundworker.aspx
  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
挽手叙旧 2024-12-09 06:01:31

当您执行此行时 backgroundWorker1.RunWorkerAsync();
它立即返回并执行下一行。您需要订阅消息框的 RunWorkerCompleted 事件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文