0

Im trying to update a progress bar in my main form from a threadpool but everything Iv'e tried so far updates the value of the progress bar but not the physical appearance of it, Tried several different ways and still can't find a solution. Can someone please point me in the right direction to update the progress bar on my form1 by 1 every time a thread has completed.

Imports System.Threading

Public Class Form1


Dim arry As New List(Of String)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        BackgroundWorker1.RunWorkerAsync()

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
End Sub


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

    Dim ii As Integer = 2000

    For a As Integer = 0 To ii
        arry.Add(a)
    Next

    ProgressBar1.Maximum = arry.Count

    ThreadPool.SetMaxThreads(4, 4)
    Dim doneEvents(arry.Count) As ManualResetEvent
    Dim r As New Random()
    For i As Integer = 0 To arry.Count

        doneEvents(i) = New ManualResetEvent(False)
        Dim f = New stuff(r.Next(20, 40), doneEvents(i))
        ThreadPool.QueueUserWorkItem(AddressOf f.ThreadPoolCallBack, i)

    Next

    For Each handle As WaitHandle In doneEvents
        handle.WaitOne()
    Next

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CheckForIllegalCrossThreadCalls = False
End Sub


Private Delegate Sub FillDelegate()

Public Sub Fill()
    If ProgressBar1.InvokeRequired Then
        ProgressBar1.BeginInvoke(New FillDelegate(AddressOf Fill))
    Else
        ProgressBar1.Increment(1)
        ProgressBar1.Refresh()

    End If
End Sub

End Class


Public Class stuff

Private _doneEvent As ManualResetEvent
Private _n As Integer

Public ReadOnly Property N() As Integer
    Get
        Return _n
    End Get
End Property

Sub New(ByVal n As Integer, ByVal doneEvent As ManualResetEvent)
    _n = n
    _doneEvent = doneEvent
End Sub


Public Sub ThreadPoolCallBack(ByVal threadContext As Object)
    Try

        Dim threadIndex As Integer = CType(threadContext, Integer)
        Console.WriteLine("thread {0} started...", threadIndex)
        Form1.Fill()
        Console.WriteLine("thread {0} finished...", threadIndex)

    Catch ex As Exception
        Console.WriteLine("error in threadPoolCallback")
        Console.WriteLine(ex.Message)
    Finally
        _doneEvent.Set()
    End Try

End Sub

End Class
  • Using `CheckForIllegalCrossThreadCalls = False` is a bad idea. You are just ignoring errors that might come back to bite you after you deploy. Use the reporting mechanism of the background worker to give feedback: http://stackoverflow.com/questions/3289920/how-to-report-progress-from-within-a-class-to-a-backgroundworker – Matt Wilko Jun 01 '16 at 13:16
  • The progressbar takes around 600-700ms to actually animate from 0-100% so if your loop takes less time than this you won't see it move. – Matt Wilko Jun 01 '16 at 13:17
  • it takes about 3 minutes for the entire process to finish, I removed a few tasks it does between the thread start and end – user3701006 Jun 01 '16 at 13:25

0 Answers0