Public Class 公共类
Declare Function GetTickCount Lib "kernel32.dll" Alias "GetTickCount" () As Integer
Public Sub 循环()
Dim 次数 As Integer = 0
Dim Start_T = GetTickCount
Do While True
Threading.Thread.Sleep(100)
'做点事占用点时间
次数 += 1
If 次数 = 50 Then
Dim Stop_T = GetTickCount
Console.WriteLine("运行50次所用时间(毫秒):{0}", Stop_T - Start_T)
次数 = 0
Start_T = GetTickCount
End If
Loop
End Sub
End Class
调用时用多线程时:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim ggl As New 公共类
Dim Thread1 As New System.Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf ggl.循环))
Thread1.IsBackground = True
Thread1.Start() ' 启动新线程
End Sub
结果前面2次运行时运行速度正常,当运行超过5个线程时,运行速度明显变慢。等待解决方案