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 (线程停止)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值