Hi,
I'm having difficulty in working with MultiThreading.
I want to add to my project a ProgressBar to show the working flow.
I'm getting combinations from many Strings, the number of combination can be really big, so it takes few seconds or minutes.
I added a ProgressBar and assigned its properties,
but I found out that it won't show the changes on the interface, so I searched the net and found that I have to use multi threads.
below you will find my code, it doesn't work with me, right now the method I'm calling "InitializtPB" is not doing anything, and when I made few changes by using "Delegate Function" it gave me what appears to be a common bug "cross thread operation not valid". Any help would be appreciated, since I'm close to my deadline, thanks in advance...
The button that starts all the process:
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim lThread As Thread
lThread = New Thread(AddressOf Me.Iterate)
lThread.IsBackground = True
lThread.Start()
End Sub
my Subroutine "Iterate":
Private Sub TheIteration()
Dim dbrec As dbRecordSet
Dim lItemsList As New Dictionary(Of String, dbRecordSet)
Dim lItem As String
Dim i As Integer
Dim k As Integer
Dim l As Integer
Dim lRecordID As String
Dim lLength As String
Dim lQty As String
Dim lPos As String
Dim TT() As String
Dim lCalculatedItemsList As New Dictionary(Of String, String())
Dim lSeperatedCalculatedItemsList As New Dictionary(Of String, String())
Dim lNbOfTubes As Long
Dim lItemQty As Long
Dim lSeperatedItemsList() As String
Dim lPossibleCuteDistributions As Dictionary(Of Integer, String())
CallInitializePBInThread(0, 100, 10, 0, "My text")
For Each lItem In lItemsList.Keys
i = 0
k = 0
dbrec = lItemsList(lItem)
lRecordID = ""
lQty = "0"
lLength = "0"
lPos = ""
ReDim TT(0)
ReDim lSeperatedItemsList(0)
Do While Not dbrec.EOF
ProjectsCls.GetItemRecordProperties(dbrec.RS(k), lRecordID, lQty, lPos, lLength)
ReDim Preserve TT(i)
TT(i) = lRecordID & ":" & lPos & ":" & lLength & ":" & lQty
For l = 1 To CInt(lQty)
lSeperatedItemsList(UBound(lSeperatedItemsList)) = lRecordID & ":" & lLength
ReDim Preserve lSeperatedItemsList(UBound(lSeperatedItemsList) + 1)
Next
i = i + 1
k = k + 1
lItemsList(lItem).MoveNext()
Loop
ReDim Preserve lSeperatedItemsList(UBound(lSeperatedItemsList) - 1)
lSeperatedCalculatedItemsList.Add(lItem, lSeperatedItemsList)
lCalculatedItemsList.Add(lItem, TT)
IncrementPB()
Next
End Sub
The Sub that initializes my ProgressBar:
Private Sub InitializePB(ByVal pMin As Integer, ByVal pMax As Integer, ByVal pStep As Integer, ByVal pValue As Integer, ByVal pText As String)
ProgressBar1.Minimum = pMin
ProgressBar1.Maximum = pMax
ProgressBar1.Value = pValue
ProgressBar1.Step = pStep
lblProgress.Text = pText
End Sub
Delegate function for InitializePB:
Private Delegate Sub DelInitializePB(ByVal pMin As Integer, ByVal pMax As Integer, ByVal pStep As Integer, ByVal pValue As Integer, ByVal pText As String)
The Sub I call inside the created thread to access the ProgressBar:
Private Sub CallInitializePBInThread(ByVal pMin As Integer, ByVal pMax As Integer, ByVal pStep As Integer, ByVal pValue As Integer, ByVal pText As String)
Dim lDel As DelInitializePB
If Me.InvokeRequired Then
lDel = New DelInitializePB(AddressOf InitializePB)
lDel.Invoke(pMin, pMax, pStep, pValue, pText)
End If
End Sub
The Sub that increments the ProgressBar Value:
Private Sub IncrementPB()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf IncrementPB))
Else
ProgressBar1.Value += 1
ProgressBar1.PerformStep()
ProgressBar1.PerformClick()
End If
End Sub
When I call "InitializePB", the exception occurs on the first code line "Progress.Visible = True" :S