后台工作人员和进度栏无法正常工作
问题描述:
嗨试图有一个进度条加载我的加密和压缩应用程序。 我想使用后台工作人员来更新压缩和加密处理的时间进度栏,但不知何故应用程序显示加密失败信息框,我已经包含在按钮,而不是成功。后台工作人员和进度栏无法正常工作
这是我的按钮
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);
}
我不知道哪里出了问题。进度条和两种加密方式都不起作用。
答
当您执行此行时backgroundWorker1.RunWorkerAsync();
它立即返回并执行下一行。您需要为您的消息框订阅RunWorkerCompleted事件。
答
- 使用string.IsNullOrEmpty(串),而不是其他==“”
- 你没有工人进度事件设置
- 看样子你通过UI设计师加入您的后台工作 - 在代码中创建这些 - 更清洁
- kmcc049是正确的 - 我没有看到一个完整的处理程序要么
- 点击此链接获取更多信息如何做到这一点。 http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
你很基本上误解BGW的工作原理。而0000的意思是。教育自己,这不是正确的地方。 –
我的建议是让编码代码在没有BackgroundWorker的情况下工作,稍后您可以将其添加到工作代码中。我还会创建一些测试代码来让Backgroundworker显示进度条增加并稍后添加编码代码。 – CodingBarfield