多线程Demo VB.net
1 Partial Class Test_MapTest 2 Inherits System.Web.UI.Page 3 4 5 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 6 7 Dim StartTime As DateTime = Now 8 9 Dim FABCStatus As New ABCStatus 10 11 For I = 1 To 100 12 Dim FABC As New ABC(I, FABCStatus) 13 Dim FThread As New Threading.Thread(AddressOf FABC.Test) 14 FThread.IsBackground = True 15 FThread.Start() 16 Next 17 18 Do While Now.Subtract(StartTime).TotalSeconds < 10 19 Threading.Thread.Sleep(100) 20 Loop 21 22 Response.Write(FABCStatus.HTML) 23 End Sub 24 25 End Class 26 27 Public Class ABCStatus 28 Public Number As Integer = 0 29 Public HTML As String = "" 30 End Class 31 32 Public Class ABC 33 34 Public ThreadID As Integer 35 Public FABCStatus As ABCStatus 36 37 Public Sub New(ByVal ThreadID As Integer, ByRef FABCStatus As ABCStatus) 38 Me.ThreadID = ThreadID 39 Me.FABCStatus = FABCStatus 40 End Sub 41 42 Public Sub Test() 43 For I = 1 To 100 44 Threading.Interlocked.Increment(FABCStatus.Number) 45 'Number = Number + 1 46 FABCStatus.HTML = FABCStatus.HTML & "<br>" & ThreadID & ": " & FABCStatus.Number 47 Threading.Thread.Sleep(100) 48 Next 49 End Sub 50 51 End Class
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步