Vb.net Thread线程实例

前端之家收集整理的这篇文章主要介绍了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 (线程停止)

猜你在找的VB相关文章