Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 报告BackgroundWorker中的进度_Vb.net - Fatal编程技术网

Vb.net 报告BackgroundWorker中的进度

Vb.net 报告BackgroundWorker中的进度,vb.net,Vb.net,在我的backgroundworker上完成操作之前,进度条会重复两三次 此代码: Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 'Load Data For i As Integer = 0 To ListData.Count Bac

在我的backgroundworker上完成操作之前,进度条会重复两三次

此代码:

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    'Load Data
    For i As Integer = 0 To ListData.Count
        BackgroundWorker1.ReportProgress((i / ListData.Count) * 100)
    Next
    If BackgroundWorker1.CancellationPending Then
        e.Cancel = True
    End If
End Sub

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

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    'set datasource of DatagridView
    ProgressBar1.Style = ProgressBarStyle.Blocks

End Sub
在我的负载表中

BackgroundWorker1.WorkerReportsProgress = True
    BackgroundWorker1.WorkerSupportsCancellation = True
    BackgroundWorker1.RunWorkerAsync()
    ProgressBar1.Style = ProgressBarStyle.Marquee

请帮帮我,你有几个错误。首先,如果要显示递增进度条,应使用:

ProgressBar1.Style = ProgressBarStyle.Continuous
在你的装载表格中。接下来,只有在检查完所有的
列表数据之后,您才能检查
BackgroundWorker1.CancellationPending
。这太晚了,您必须在循环的每个迭代中检查它。我还真的怀疑您是否希望循环从0转到
ListData.Count
;您可能希望从1开始,或者转到
ListData.Count-1
。从你的问题我看不出来。您的循环应该更像这样:

For i as Integer = 0 To ListData.Count - 1
    If BackgroundWorker1.CancellationPending Then
        e.Cancel = True
        Exit For
    Else
        ' You should be doing some work here, not just calling ReportProgress
        BackgroundWorker1.ReportProgress(100 * (i+1) / ListData.Count)
    End If
Next

另一个错误是计算
(i/ListData.Count)*100
i
ListData.Count
都是整数,因此它们的除法始终为零,直到最后为1。相反,将分子乘以100得到百分比。

您有几个错误。首先,如果要显示递增进度条,应使用:

ProgressBar1.Style = ProgressBarStyle.Continuous
在你的装载表格中。接下来,只有在检查完所有的
列表数据之后,您才能检查
BackgroundWorker1.CancellationPending
。这太晚了,您必须在循环的每个迭代中检查它。我还真的怀疑您是否希望循环从0转到
ListData.Count
;您可能希望从1开始,或者转到
ListData.Count-1
。从你的问题我看不出来。您的循环应该更像这样:

For i as Integer = 0 To ListData.Count - 1
    If BackgroundWorker1.CancellationPending Then
        e.Cancel = True
        Exit For
    Else
        ' You should be doing some work here, not just calling ReportProgress
        BackgroundWorker1.ReportProgress(100 * (i+1) / ListData.Count)
    End If
Next

另一个错误是计算
(i/ListData.Count)*100
i
ListData.Count
都是整数,因此它们的除法始终为零,直到最后为1。相反,将分子乘以100得到百分比。

我假设
循环应该从
1
ListData.Count
;否则,第一次迭代表明没有完成任何工作(
100*0/ListData.Count
等于零)。多谢。mm progressbar在BackgroundWorker1_RunWorkerCompleted上采用了什么风格?很好的定位@Karl,这个问题模棱两可。我将其从
0
更改为
ListData.Count-1
@Wfgo,为什么要更改
ProgressBar
的样式?我决不会那样做。你想得到什么效果?@DourHighArch,嗯,这个代码的样式continuos progressbar完成了“35%”(例如)。。将样式选框设置为块,progressbar完成“90%”,这看起来不错。。我不明白发生了什么我假设
For
循环应该从
1
转到
ListData.Count
;否则,第一次迭代表明没有完成任何工作(
100*0/ListData.Count
等于零)。多谢。mm progressbar在BackgroundWorker1_RunWorkerCompleted上采用了什么风格?很好的定位@Karl,这个问题模棱两可。我将其从
0
更改为
ListData.Count-1
@Wfgo,为什么要更改
ProgressBar
的样式?我决不会那样做。你想得到什么效果?@DourHighArch,嗯,这个代码的样式continuos progressbar完成了“35%”(例如)。。将样式选框设置为块,progressbar完成“90%”,这看起来不错。。我不明白发生了什么事