当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Thread类代码示例

本文整理汇总了VB.NET中System.Threading.Thread的典型用法代码示例。如果您正苦于以下问题:VB.NET Thread类的具体用法?VB.NET Thread怎么用?VB.NET Thread使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Thread类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: ThreadExample

' 导入命名空间
Imports System.Threading

' Simple threading scenario:  Start a Shared method running
' on a second thread.
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts.
    ' It loops ten times, writing to the console and yielding 
    ' the rest of its time slice each time, and then ends.
    Public Shared Sub ThreadProc()
        Dim i As Integer
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next
    End Sub

    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.
        Dim t As New Thread(AddressOf ThreadProc)

        ' 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.
        t.Start()
        'Thread.Sleep(0)

        Dim i As Integer
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.")
        Console.ReadLine()
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:44,代码来源:Thread

示例2: Example

' 导入命名空间
Imports System.Diagnostics
Imports System.Threading

Module Example
   Public Sub Main()
      Dim th As New Thread(AddressOf ExecuteInForeground)
      th.Start()
      Thread.Sleep(1000)
      Console.WriteLine("Main thread ({0}) exiting...", Thread.CurrentThread.ManagedThreadId) 
   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
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:28,代码来源:Thread

输出:

Thread 3: Running, Priority Normal
Thread 3: Elapsed 0.00 seconds
Thread 3: Elapsed 0.51 seconds
Main thread (1) exiting...
Thread 3: Elapsed 1.02 seconds
Thread 3: Elapsed 1.53 seconds
Thread 3: Elapsed 2.05 seconds
Thread 3: Elapsed 2.55 seconds
Thread 3: Elapsed 3.07 seconds
Thread 3: Elapsed 3.57 seconds
Thread 3: Elapsed 4.07 seconds
Thread 3: Elapsed 4.58 seconds

示例3: Example

' 导入命名空间
Imports System.Diagnostics
Imports System.Threading

Module Example
   Public Sub Main()
      Dim th As New Thread(AddressOf ExecuteInForeground)
      th.Start(4500)
      Thread.Sleep(1000)
      Console.WriteLine("Main thread ({0}) exiting...", Thread.CurrentThread.ManagedThreadId) 
   End Sub
   
   Private Sub ExecuteInForeground(obj As Object)
      Dim interval As Integer
      If IsNumeric(obj) Then
         interval = CInt(obj)
      Else
         interval = 5000
      End If   
      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 <= interval
      sw.Stop() 
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:34,代码来源:Thread

输出:

Thread 3: Running, Priority Normal
Thread 3: Elapsed 0.00 seconds
Thread 3: Elapsed 0.52 seconds
Main thread (1) exiting...
Thread 3: Elapsed 1.03 seconds
Thread 3: Elapsed 1.55 seconds
Thread 3: Elapsed 2.06 seconds
Thread 3: Elapsed 2.58 seconds
Thread 3: Elapsed 3.09 seconds
Thread 3: Elapsed 3.61 seconds
Thread 3: Elapsed 4.12 seconds

示例4: Example

' 导入命名空间
Imports System.Threading

Module Example
   Private lock As New Object()
                    
   Public Sub Main()
      ThreadPool.QueueUserWorkItem(AddressOf ShowThreadInformation)
      Dim th1 As New Thread(AddressOf ShowThreadInformation)
      th1.Start()
      Dim th2 As New Thread(AddressOf ShowThreadInformation)
      th2.IsBackground = True
      th2.Start()
      Thread.Sleep(500)
      ShowThreadInformation(Nothing) 
   End Sub
   
   Private Sub ShowThreadInformation(state As Object)
      SyncLock lock
         Dim th As Thread = Thread.CurrentThread
         Console.WriteLine("Managed thread #{0}: ", th.ManagedThreadId)
         Console.WriteLine("   Background thread: {0}", th.IsBackground)
         Console.WriteLine("   Thread pool thread: {0}", th.IsThreadPoolThread)
         Console.WriteLine("   Priority: {0}", th.Priority)
         Console.WriteLine("   Culture: {0}", th.CurrentCulture.Name)
         Console.WriteLine("   UI culture: {0}", th.CurrentUICulture.Name)
         Console.WriteLine()
      End SyncLock
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:30,代码来源:Thread

输出:

Managed thread #6:
Background thread: True
Thread pool thread: False
Priority: Normal
Culture: en-US
UI culture: en-US

Managed thread #3:
Background thread: True
Thread pool thread: True
Priority: Normal
Culture: en-US
UI culture: en-US

Managed thread #4:
Background thread: False
Thread pool thread: False
Priority: Normal
Culture: en-US
UI culture: en-US

Managed thread #1:
Background thread: False
Thread pool thread: False
Priority: Normal
Culture: en-US
UI culture: en-US

示例5: Example

' 导入命名空间
Imports System.Diagnostics
Imports System.Threading

Module Example
   Public Sub Main()
      Dim th As New Thread(AddressOf ExecuteInForeground)
      th.IsBackground = True
      th.Start()
      Thread.Sleep(1000)
      Console.WriteLine("Main thread ({0}) exiting...", Thread.CurrentThread.ManagedThreadId) 
   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
End Module
开发者ID:VB.NET开发者,项目名称:System.Threading,代码行数:29,代码来源:Thread

输出:

Thread 3: Background, Priority Normal
Thread 3: Elapsed 0.00 seconds
Thread 3: Elapsed 0.51 seconds
Main thread (1) exiting...


注:本文中的System.Threading.Thread类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。