【问题标题】:vb net backgroundworker + progress barvb net backgroundworker + 进度条
【发布时间】:2014-04-22 05:30:46
【问题描述】:

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

我的程序有 6 个函数/子运行查询需要很长时间才能完成,关于 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

做事

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. 函数在完成前停止

对于第二个问题,我认为是因为我在 Do_work 事件中使用的 for loop

我不知道我拥有的每个函数的运行时间,所以我在for loop中使用随机整数

你能帮我改正我的程序吗?非常感谢你.. :)

【问题讨论】:

    标签: vb.net progress-bar backgroundworker


    【解决方案1】:

    首先,您应该在调用RunWorkerAsync 之前设置ProgressBarVisible 属性。

    其次,您应该将i Mod 10000000 与某物进行比较。

    第三,如果i 上升到 10000000,那么 i / 100 将超过 100。

    【讨论】:

    • formLoad 上,我已经将progressBar 属性设置为True.. 然后在backgroundworker 事件progressChanged.. 上将其设置为false出现..你能告诉我如何比较..我不明白逻辑,如何在doWork事件中设置我的程序的持续时间,因为我不知道如何获得我的函数的总运行时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多