Backgroundworker ReportProgress Firing但ProgressBar没有变化

时间:2011-08-15 19:51:16

标签: vb.net progress-bar backgroundworker

我的表单上有一个没有更新的进度条。

单击“发送电子邮件”按钮时,我执行以下操作:

Public Sub SendMail()
    If CheckSettings() = False Then Exit Sub
    BackUpEbillFile()
    LockForm(True)
    StatusBars(1, "Sending emails...")
    ProgressBar1.Maximum = intInvoicesToSend  
    BackgroundWorker1.RunWorkerAsync()
End Sub

我有以下事件:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    e.Result = SendBills()
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Console.WriteLine("I DID IT MA!!!!1 status: " & ProgressBar1.Value)
    ProgressBar1.PerformStep()
    Console.WriteLine("I DID IT MA!!!!2 status: " & ProgressBar1.Value)
End Sub

在SendBills中,在邮件类中,我这样做:

smtp.Send(msg)
Console.WriteLine("I DID IT PA!!!! count: " & iCount)
frmBilling.BackgroundWorker1.ReportProgress(iCount)

我的问题在于ProgressChanged中的ProgressBar1.PerformStep()。我正在改变进度,但Progressbar1没有改变。它保持为零。

  • intInvoicesToSend是16
  • ProgressBar1.Minimum = 0
  • ProgressBar1.Maximum = intInvoicesToSend(16)
  • ProgressBar1.Step = 1

这是我在console.writelines的控制台:

我发布了它!数:0

I DID IT MA !!!! 1状态:0

I DID IT MA !!!! 2状态:1

我发布了它!数:1

I DID IT MA !!!! 1状态:1

I DID IT MA !!!! 2状态:2

我发布了它!数:2

I DID IT MA !!!! 1状态:2

I DID IT MA !!!! 2状态:3

我发布了它!数:3

I DID IT MA !!!! 1状态:3

I DID IT MA !!!! 2状态:4

谁能告诉我我做错了什么? 谢谢!

2 个答案:

答案 0 :(得分:1)

我怀疑你是在错误的实例上调用ReportProgress方法。

见这一行:

frmBilling.BackgroundWorker1.ReportProgress(iCount)

如果直接在表单中定义BackgroundWorker,则可以不使用frmBilling部分,只需将其指定为:

BackgroundWorker1.ReportProgress(iCount)

你拥有它的方式可能导致ReportProgress在错误的实例上被调用...

答案 1 :(得分:0)

试试这个......

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Console.WriteLine("I DID IT MA!!!!1 status: " & ProgressBar1.Value)
    ProgressBar1.PerformStep()
    Application.DoEvents()
    Console.WriteLine("I DID IT MA!!!!2 status: " & ProgressBar1.Value)
End Sub