當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。