Imports System.Diagnostics
Imports System.Threading
Module Example
Public Delegate Sub ThreadStart()
Public Sub Main()
ThreadPool.QueueUserWorkItem(AddressOf ExecuteInForeground)
Dim th1 As New Thread(AddressOf ExecuteInForeground)
th1.Start()
Dim th2 As New Thread(AddressOf ExecuteInForeground)
th2.IsBackground = True
th2.Start()
Thread.Sleep(500)
ThreadPool.QueueUserWorkItem(AddressOf WriteNumber)
Dim th3 As New Thread(AddressOf WriteNumber)
th3.IsBackground = True
th3.Start()
Thread.Sleep(500)
ExecuteInForeground()
'WriteNumber()
MsgBox("所有线程都over了")
End Sub
Private Sub ExecuteInForeground()
Dim start As DateTime = DateTime.Now
Dim sw As Stopwatch = Stopwatch.StartNew()
Console.WriteLine("Thread {0}: {1}, Priority {2}", _
Thread.CurrentThread.ManagedThreadId, _
Thread.CurrentThread.ThreadState, _
Thread.CurrentThread.Priority)
Do
Console.WriteLine("Thread {0}: Elapsed {1:N2} seconds", _
Thread.CurrentThread.ManagedThreadId, _
sw.ElapsedMilliseconds / 1000)
Thread.Sleep(500)
Loop While sw.ElapsedMilliseconds <= 5000
sw.Stop()
End Sub
Private Sub WriteNumber()
Dim i As Integer = 0
Do
i += 1
Console.WriteLine(i.ToString)
Loop While i <= 9000
End Sub
End Module