?
Solved

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

Posted on 2014-11-20
3
Medium Priority
?
721 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
0
Comment
Question by:Lynchie435
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:Lynchie435
ID: 40454776
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
0
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 total points
ID: 40454970
Then you need to move your long running process; i.e., the call to the web service inside of your background worker.  The reason for this is because the long running code is blocking your main ui thread, meaning that updates from the background worker will only get ran once the main ui thread finishes the blocking code.  Something like:
Imports System.ComponentModel

Public Class Form1
	Private worker As New BackgroundWorker

	Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		ProgressBar1.Value = 99
		ProgressBar1.Style = ProgressBarStyle.Marquee
		worker.RunWorkerAsync()
	End Sub

	Private Sub OnLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		worker.WorkerReportsProgress = True
		AddHandler worker.DoWork, AddressOf OnDoWork
		AddHandler worker.ProgressChanged, AddressOf OnProgressChanged
		AddHandler worker.RunWorkerCompleted, AddressOf OnRunWorkerCompleted
	End Sub

	Private Sub OnDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
		If hubURLtxtbx.Text = "LIVE" Then
			XStreamLive()
		Else
			XStreamUAT()
		End If
		worker.ReportProgress(100)
	End Sub

	Private Sub OnProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
		ProgressBar1.Style = ProgressBarStyle.Continuous
		ProgressBar1.Value = e.ProgressPercentage
	End Sub

	Private Sub OnRunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
		MsgBox("Completed")
	End Sub
End Class

Open in new window


To add further confusion to the fire, in your code above, the XStream has to give state in order to show progress, since it does not, then your background worker also gets blocked, so as it stands right now you have two options to handle this situation.  Setup your progress bar to be a marquee bar as I have done above.

Or call your web service asynchronously so that you can provided continual updates based upon your globals, as you have done.

-saige-
0
 

Author Closing Comment

by:Lynchie435
ID: 40455030
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
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question