Vb.net Thread线程实例

Imports System.Threading

Class Class1

'Public Shared Sub Main(ByVal args() As String)
' 'Application.Run(New Form1())
' '' Get the path that stores favorite links.
' Shell("notepad",AppWinStyle.normalFocus)
'End Sub 'Main

Shared t As Thread
Public Shared Sub Main()
Console.WriteLine("Main thread: Start a second thread.")
' The constructor for the Thread class requires a ThreadStart
' delegate. The Visual Basic AddressOf operator creates this
' delegate for you.
t = New Thread(AddressOf ThreadProc1)

' Start ThreadProc. Note that on a uniprocessor,the new
' thread does not get any processor time until the main thread
' is preempted or yields. Uncomment the Thread.Sleep that
' follows t.Start() to see the difference.
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
t.Start()
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
Thread.Sleep(0)
Dim i As Integer
Console.WriteLine("Main thread:---START")
For i = 1 To 4
Console.WriteLine("Main thread: Do some work.")
Threading.Thread.Sleep(0)
Next

Console.WriteLine("Main thread: Call Join(),to wait until ThreadProc ends.")
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
t.Join()
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.")
Console.ReadLine()
t = New Thread(AddressOf ThreadProc2)

' Start ThreadProc. Note that on a uniprocessor,the new
' thread does not get any processor time until the main thread
' is preempted or yields. Uncomment the Thread.Sleep that
' follows t.Start() to see the difference.
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
t.Start()
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
t.Join()
Console.WriteLine("ThreadState:" + CStr(t.ThreadState))
t = New Thread(AddressOf ThreadProc3)
t.Start()
t.Join()
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.")
Console.ReadLine()
End Sub

Public Shared Function ThreadProc1() As Integer
Console.WriteLine("ThreadProc1---START")
For i As Integer = 0 To 10
Console.WriteLine(CStr(i))
Next
Console.WriteLine("ThreadProc1---END")
End Function

Public Shared Function ThreadProc2() As Integer
Console.WriteLine("ThreadProc2---START")
For i As Integer = 0 To 10
Console.WriteLine(CStr(i))
Next
Console.WriteLine("ThreadProc2---END")
End Function

Public Shared Function ThreadProc3() As Integer
Console.WriteLine("ThreadProc3---START")
For i As Integer = 0 To 10
Console.WriteLine(CStr(i))
Next
Console.WriteLine("ThreadProc3---END")
End Function

End Class 'MyProcess


控件台输出结果:

Main thread: Start a second thread.
ThreadState:8
ThreadProc1---START
0
1
2
3
4
5
6
7
8
9
10
ThreadProc1---END
ThreadState:0
Main thread:---START
Main thread: Do some work.
Main thread: Do some work.
Main thread: Do some work.
Main thread: Do some work.
Main thread: Call Join(),to wait until ThreadProc ends.
ThreadState:16
ThreadState:16
Main thread: ThreadProc.Join has returned. Press Enter to end program.
ThreadState:8
ThreadState:0
ThreadProc2---START
0
1
2
3
4
5
6
7
8
9
10
ThreadProc2---END
ThreadState:16
ThreadProc3---START
0
1
2
3
4
5
6
7
8
9
10
ThreadProc3---END
Main thread: ThreadProc.Join has returned. Press Enter to end program.

ThreadState:8 =Unstart(没运行)

ThreadState:0=running(线程运行)

ThreadState:16=stoped (线程停止)

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...