We help IT Professionals succeed at work.

Web Service Call - Progress Bar with BackGroundWorker Not Working As Intended VB.NET

979 Views
Last Modified: 2014-11-20
I am creating an application in VB.NET where users are able to attach XML via an OpenFileDialog and fire it at the Web Service.

I wish to have a progress bar at the bottom as at the moment, the GUI hangs and users think it has crashed while it is waiting for a response.

My code is below:

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 My_BgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        For i As Integer = 0 To GlobalVariables.m_CountTo
            ' Has the background worker be told to stop?
            If BackgroundWorker1.CancellationPending Then
                ' Set Cancel to True
                e.Cancel = True
                Exit For
            End If
            System.Threading.Thread.Sleep(1000) ' Sleep for 1 Second
            ' Report The progress of the Background Worker.
            BackgroundWorker1.ReportProgress(CInt((i / GlobalVariables.m_CountTo) * 100))
        Next
    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

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
 ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
 Handles BackgroundWorker1.RunWorkerCompleted
        If e.Cancelled Then
            MsgBox("Cancelled")
        Else
            MsgBox("Completed")
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    
        ' Is the Background Worker do some work?
        If BackgroundWorker1.IsBusy Then
            'If it supports cancellation, Cancel It
            If BackgroundWorker1.WorkerSupportsCancellation Then
                ' Tell the Background Worker to stop working.
                BackgroundWorker1.CancelAsync()
            End If
        End If
    End Sub

Open in new window


Basically at the moment with the code as above the progress bar is updating AFTER I have received the XML response from the web service.

I want to have it so that the call is made and when the call is made the progress is updating, then when the call is successfully the bar is completely full?

Can anyone shed any light on this for me at all please?

On debug it appears the 'hanging' is when the XStreamUAT/XStreamLIVE Sub is called and it uses the Web Service Reference Client Object to send the XML to the Service.

Cheers,

James
Comment
Watch Question

Author

Commented:
I have moved the

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

Open in new window


I have moved the IF Statement instead the BackGroundWOrker DoWork Event however it STILL only moves the progress bar after the XML has fired and been returned.

I want it to fire and the progress go up while it is waiting for a return.

Cheers,

James
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi again saige :)

Yeah I toyed with them both, the updating the globals was working but I do feel the marquee would be better as it at least gives off the impression something is occurring rather than all the sudden the bar filling up.

I understand what your saying now though, because my XStream doesn't return a state I have nothing to progress against.

Muchos.

J

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.