I have the following script which creates 2 threads as you can see.The thing is I would like this code to be tidy-er and the thread making better (such as a loop) but I haven’t been able to manage to create it.
Now i dim thread1 and thread 2 and have separate functions for both.
I am really new to vb.net so please dont shoot be down.
How would I make a public MaxThreads as integer = 2 (or whatever)
and then be able to
for i as 1 to MaxThreads
‘ what now is in function startthread1
next
Public Class UnCheckedItemReader
Public ForceStop As Boolean = False ' to force shutdown in case the application is Closed
Public UnCheckedItems As Integer ' to check and report
Public QueueList As New Dictionary(Of Integer, Integer) ' this list will contain the addressbook ID and the number of the thread holding it
Public QueueProcessed As New Dictionary(Of Integer, Integer) ' this will aloow us to update the Clientlistview
Private ds As New DataSet
Private da As SqlCeDataAdapter = New SqlCeDataAdapter()
Private connection As New ConnectionClass
Private table As String = "uncheckItems"
'threads section - we have 2 threads
Dim Thread1 As Threading.Thread
Dim Thread2 As Threading.Thread
Public Sub Controller()
Do While ForceStop = False
If QueueList.Count < 1 Then
GetDatabaseSqlClientListUnCheckedItems()
PopulateQueueList()
StartThreads()
End If
Threading.Thread.Sleep(5000)
Console.WriteLine("New Round")
Loop
End Sub
Private Sub StartThreads()
StartThread1()
StartThread2()
End Sub
Private Sub StartThread1()
Dim DS As New RelianProcess
DS.id = 5
DS.ThreadId = 1
DS.QueueList = QueueList
AddHandler DS.ShowError, AddressOf ShowErrorThread1
AddHandler DS.Stopit, AddressOf StopThread1
Thread1 = New System.Threading.Thread(AddressOf DS.StartTimer)
Thread1.IsBackground = True
Try
Thread1.Start()
Catch e As ThreadStateException
Console.WriteLine("Caught: {0}", e.Message)
Catch ex As Exception
Console.WriteLine("Caught: {0}", ex.Message)
End Try
End Sub
Private Sub StartThread2()
Dim DS As New RelianProcess
DS.id = 5
DS.ThreadId = 2
DS.QueueList = QueueList
AddHandler DS.ShowError, AddressOf ShowErrorThread1
AddHandler DS.Stopit, AddressOf StopThread1
Thread2 = New System.Threading.Thread(AddressOf DS.StartTimer)
Thread2.IsBackground = True
Try
Thread2.Start()
Catch e As ThreadStateException
Console.WriteLine("Caught: {0}", e.Message)
Catch ex As Exception
Console.WriteLine("Caught: {0}", ex.Message)
End Try
End Sub
Private Sub ShowErrorThread1(ByVal e As Exception, ByVal id As Integer)
Console.Write(e.ToString)
QueueList.Remove(id)
Console.Write(vbCrLf + "Removed from Mother queuelist " + CStr(id))
QueueProcessed.Add(id, CInt(GetCurrentUnixTimestamp()))
Console.Write(vbCrLf + "Added to processed list " + CStr(id))
End Sub
Private Sub ShowErrorThread2(ByVal e As Exception, ByVal id As Integer)
Console.Write(e.ToString)
QueueList.Remove(id)
Console.Write(vbCrLf + "Removed from Mother queuelist " + CStr(id))
QueueProcessed.Add(id, CInt(GetCurrentUnixTimestamp()))
Console.Write(vbCrLf + "Added to processed list " + CStr(id))
End Sub
Private Sub StopThread1()
Thread1.Abort()
End Sub
Private Sub StopThread2()
Thread2.Abort()
End Sub
Private Sub PopulateQueueList()
Dim i As Integer = 1
If ds.Tables(table).Rows.Count > 0 Then
For Each irow As DataRow In ds.Tables(table).Rows
Try
If QueueList.ContainsKey(CInt(irow(0))) = False Then
i = i + 1
If i = 3 Then
i = 1
End If
QueueList.Add(CInt(irow(0)), i)
Console.WriteLine(ControlChars.NewLine + "Added item {0} to thread {1}", irow(0), i)
End If
Catch e As Exception
Console.WriteLine(ControlChars.NewLine + "Exception Raised. The following error occured : {0}", e.Message)
End Try
Next
End If
End Sub
Instead of declaring each thread individually, use a Collection of Threads. Something along these lines: