SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Progress Bar (BackgroundWorker)

    Dear Team,

    I spent over 25 hours on this and havnt reached my goal. Please suggest.

    I have a button (btnUpdate) on a form where I am calling a function. The function updates local sql server table using web services. But the progress bar runs the code (or function) 100 times. Attached text file is the function. All the background and progress stuff codes are shown here.

    Private Sub btnSyncNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSyncNew.Click
    Me.BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
    For i As Integer = 1 To 100
    'Raise the ProgressChanged event in the UI thread.
    worker.ReportProgress(i, i & " iterations complete")
    'Perform some time-consuming operation here.
    Dim cSessionID = Me.txtSessionID.Text
    Dim objMySlip As New clsSyncSlip()
    Dim sResult As Integer
    sResult = objMySlip.syncSlipNew(cSessionID)
    'MsgBox("Total " & sResult & " slip(s) have been updated")
    Threading.Thread.Sleep(0)
    Next i
    MsgBox("done")
    End Sub


    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.ProgressBar1.Value = e.ProgressPercentage
    Me.Label1.Text = TryCast(e.UserState, String)
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Me.Label1.Text = "Operation complete"
    End Sub
    Attached Files Attached Files

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It takes about a second per record when I run the code without the
    progress bar. lets say I have 20 records that code finds to update. I
    takes about 15~ seconds to do the job.

    with progress bar
    For the same task it will run the code and first time I will see that
    20 records have been updated but only 1 bar moves to right. Then it
    runs the code again and again for many times and i can see the
    progress bar moving towards right.


    I hope it makes sense now.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read fourm somewhere and it's about progress bar without knowing how
    long the process will last. But I am not sure how it will work. I get
    the part where I have set the property of progress bar min and max (or
    is it someplace in the code). LOST...

    First, you need to set initial values, declare a counter and setup the
    timer :


    Code:
    progressBar.Minimum = 0
    progressBar.Maximum = 100


    _progressCounter = 0


    _timer = New Timer()
    _timer.Interval = 50
    AddHandler _timer.Tick, AddressOf Me.Timer_Elapsed
    _timer.Enabled = True


    Then, in timer event handler insert the following code


    Code:
    Private Sub Timer_Elapsed(ByVal sender As Object, ByVal args As
    EventArgs)
    If progressBar.Value = progressBar.Maximum
    Return
    End If


    _progressCounter = _progressCounter + 1
    progressBar.Value = CType((Math.Log(CType(_progressCounter,
    double)) / Math.Log(100)) * 100), Integer)
    End Sub


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •