|
Imports System.Threading
Module Module1
Sub Main() Dim x As Integer = 0 For x = 0 To 3 Dim newThread As New Thread(AddressOf Spawn) newThread.Start() Next
Console.WriteLine("Done") End Sub
<MTAThread()> Public Sub Spawn() 'One Test to just start machine threads 'Test.yeah()
'Start a new thread to do some work and waite for a response MyNewThread.MyThread() End Sub End Module
Public Class Test
<MTAThread()> _ Public Shared Sub yeah() Dim newThread As New Thread(AddressOf Work.DoWork) newThread.Start()
' To start a thread using an instance method for the thread ' procedure, use the instance variable and method name when ' you create the ThreadStart delegate. Visual Basic expands ' the AddressOf expression to the appropriate delegate ' creation syntax: ' New ThreadStart(AddressOf w.DoMoreWork) ' Dim w As New Work() w.Data = 42 newThread = New Thread(AddressOf w.DoMoreWork) newThread.Start() End Sub End Class
Public Class Work Public Shared Sub DoWork() Console.WriteLine("Static thread procedure.") End Sub
Public Data As Integer
Public Sub DoMoreWork() Console.WriteLine("Instance thread procedure. Data={0}", Data) End Sub End Class
Public Class MyNewThread Private Delegate Sub m_delMyDelegate() <MTAThread()> Public Shared Sub MyThread() Dim delThread1 As m_delMyDelegate = AddressOf GetComputerInfo Dim ar1 As IAsyncResult Dim a_WaitHandles(0) As System.Threading.WaitHandle
ar1 = delThread1.BeginInvoke(Nothing, Nothing) a_WaitHandles(0) = ar1.AsyncWaitHandle System.Threading.WaitHandle.WaitAll(a_WaitHandles, 30000, False)
If ar1.IsCompleted Then delThread1.EndInvoke(ar1) End Sub
Private Shared Sub GetComputerInfo() Threading.Thread.Sleep(29999) End Sub End Class |