Web服务调用 - 使用BackGroundWorker的进度条不能用作预期的VB.NET

时间:2014-11-20 09:50:50

标签: vb.net web-services

我在VB.NET中创建一个应用程序,用户可以通过OpenFileDialog附加XML并在Web服务上激活它。

我希望在底部有一个进度条,GUI挂起,用户认为它在等待响应时已经崩溃。

我的代码如下:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    BackgroundWorker1.RunWorkerAsync()

End Sub

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

    For I = 0 To 100 'Telling the program to count from 0 - 200

        If hubURLtxtbx.Text = "LIVE" Then
            XStreamLive()
        Else
            XStreamUAT()
        End If

        If BackgroundWorker1.CancellationPending = True Then
            e.Cancel = True
            Exit For
        Else
            'Telling the program to report the progress done by the ReportProgress
            BackgroundWorker1.ReportProgress(I)
            'Telling the program to stop after advancing one integer for 1 second.
            System.Threading.Thread.Sleep(1000)
        End If

    Next
End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles BackgroundWorker1.ProgressChanged

        ProgBar.Value = e.ProgressPercentage

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted
    ProgBar.Value = 100
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    BackgroundWorker1.CancelAsync()
    BackgroundWorker1.Dispose()
End Sub

基本上目前使用上面的代码,进度条正在更新,之后我收到了来自Web服务的XML响应。

我想拥有它以便进行呼叫,并且当进行呼叫时进度正在更新,然后当呼叫成功时,条形图已完全填满?

有人可以为我解释这个吗?

干杯,

詹姆斯

更新

好的,所以我改变了一些代码,现在当返回XML时,进度条最大值为100但它仍然冻结了GUI并且没有增加'从我点击按钮的那一刻起,它就会在收到XML时一次性填充进度条。

更新代码

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    BackgroundWorker1.RunWorkerAsync()

    If hubURLtxtbx.Text = "LIVE" Then
        XStreamLive()
    Else
        XStreamUAT()
    End If

End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles BackgroundWorker1.ProgressChanged

    If GlobalVariables.response = "" Then
        ProgBar.Value = e.ProgressPercentage
    Else
        ProgBar.Value = 100
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

我将Subs移动到BackGroundWorker的DoWork中,因为服务没有返回处理数据的“状态”,我可以将任何内容返回到我的变量来更新进度条。

因此我决定使用Marquee Progress Bar Style来显示GUI正在执行某些操作然后100%使用流程结束时的Continuous Style来显示它已完成。

Ĵ