vb net backgroundworker +进度条

时间:2014-04-22 05:30:47

标签: vb.net progress-bar backgroundworker

我想创建自动进度条,当我运行程序时会弹出...

我的程序有6个函数/ sub运行查询需要很长时间才能完成,关于input约为1-2分钟

我正在使用backgroundworker处理线程并将其与progressbar

组合

这是我的代码

的RunWorkerAsync

Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
    BackgroundWorker1.WorkerReportsProgress = True
    BackgroundWorker1.RunWorkerAsync()
End Sub

Do_work

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    For i As Integer = 0 To 100000000
        create_tree()
        localtree()
        localfrek()
        create_combination()
        showresult()
        If i Mod 10000000 Then
            BackgroundWorker1.ReportProgress(i / 100)
        End If
    Next


End Sub

ProgressChanged

 Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    ProgressBar1.Visible = True
    ProgressBar1.Value = e.ProgressPercentage
End Sub

RunWorkerCompleted

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MessageBox.Show("Task completed!")
End Sub

问题是

  1. the progress bar没有弹出
  2. 功能在完成前停止
  3. 第二个问题我认为是因为我在for loop事件中使用的Do_work

    我不知道每个函数的经过时间,所以我在for loop中使用随机整数

    你可以帮我纠正我的程序吗?非常感谢.. :))

1 个答案:

答案 0 :(得分:0)

首先,您应该在致电Visible之前设置ProgressBar的{​​{1}}属性。

其次,您应该将RunWorkerAsync与某些内容进行比较。

第三,如果i Mod 10000000达到10000000,则i将超过100。