我有以下脚本,你可以看到创建2个线程。事情是我希望这个代码是整洁的,线程做得更好(比如一个循环)但是我无法管理创造它。
现在我将线程1和线程2调暗,并且两者都有单独的功能。 我对vb.net很新,所以请不要拍下来。
如何将公共MaxThreads设为整数= 2(或其他) 然后能够 因为我是1到MaxThreads '现在是什么功能startthread1 下
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
答案 0 :(得分:0)
不是单独声明每个线程,而是使用Thread of Threads。这些方面的东西:
Dim m_threads As New List(Of Threading.Thread)
Dim m_maxThreads As Int32 = 5
Private Sub StartAllThreads()
For pos As Int32 = 1 To m_maxThreads
StartThread(pos)
Next
End Sub
Private Sub StartThread(p_threadID As Int32)
Dim tempThread As Threading.Thread
Dim DS As New RelianProcess
DS.id = 5
DS.ThreadId = p_threadID
DS.QueueList = QueueList
tempThread = New System.Threading.Thread(AddressOf DS.StartTimer)
m_threads.Add(tempThread)
AddHandler DS.ShowError, AddressOf ShowErrorThread1
AddHandler DS.Stopit, AddressOf StopThread1
tempThread.IsBackground = True
Try
tempThread.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