后台工作者和进度条无法正常工作
您好,我试图为我的加密和压缩应用程序加载一个进度条。 我正在尝试使用后台工作人员来更新压缩和加密处理所需时间的进度条,但不知何故,应用程序显示我已包含在按钮内的加密失败消息框,而不是成功。
这是我的按钮的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行此行时
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.