我的程序存在一个小问题,如下所示,我想使用多个线程,但我必须将每个线程调暗并将每个线程都解决到特定的子线,我之前已经完成了这个但是我很久没有使用VB.Net了,我忘了怎么样,有什么可能的帮助吗?感谢任何阅读/帮助的人。
Imports System.Threading
Public Class Form1
Dim thread As System.Threading.Thread
Dim ct as boolean = True
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
thread = New System.Threading.Thread(AddressOf Main)
thread.Start()
End Sub
Sub Main()
Do Until ct = False
'Executes something
Loop
End Sub
End Class
这是一个例子
Dim x As Integer = 1
Private Sub asd()
Do Until x = 50
Dim thread & x As System.Threading.Thread
thread & x = New System.Threading.Thread(AddressOf Main)
thread & x.Start()
x = x + 1
Loop
End Sub
上面是一个我想要50个线程的例子,这不是它的想法。
答案 0 :(得分:1)
这几乎肯定是一个坏主意;许多线程不一定等于更好的性能。
...但这是你的代码需要看的方式:
Private Threads As New List(Of Thread)
Private Sub asd()
For i As Integer = 1 To 50
Dim t As New System.Threading.Thread(AddressOf Main)
Threads.Add(t)
t.Start()
Next
End Sub
答案 1 :(得分:0)
我只是将它们添加到List
对象:
Dim Threads As New List(Of System.Threading.Thread)
Private Sub asd()
For X As Integer = 0 To 49
Threads.Add(New System.Threading.Thread(AddressOf Main))
Threads(x).Start
Next
End Sub
注意
Threads
使用零起始索引。
答案 2 :(得分:-1)
Dim Threads As New List(Of System.Threading.Thread)
Private Sub asd()
For X As Integer = 0 To 49
Threads.Add(New System.Threading.Thread(AddressOf Main))
Threads(x).Start
Next
End Sub