Threading*:
Imports System.IO
Imports System.Thread
Fundamentals: Premptive multitasking means that thread can be suspended at alsmot any time and another one can be give CPU time. In older versions of windows, we had cooperative multitasking, where each thread had to ask to be suspended explicitily.
When to use Threads: Each thread maintains a private set of structures that the OS uses to save information (thread contentent). When the thread isn’t running, icluding the values of CPU reisters at the time when the thread was suspended and the process was allowed to create another thread. It also maintains it’s exception handler and the priority values. OS scheduler taken CPU and memory to manage threads, so running too many threads drains the resources. Threads can compete for shared resources. We must synchronize access to resources or run into trouble. SyncLock is thus provide to deal with this issue.
Creating Threads: System.Threading.Thread offers all methods and properties needed to create and manage threads. To create a new thread, instientiate a new new thread object and invoke its start method. The thread object constructor requires one argument, a start delegate object that points to the routine that runs when the thread starts. Such a routine must have a sub without any arguments.
Sub TestThread()
Dim t As New Thread(New ThreadStart(AddressOf DoTheTask))
Dim szMsg As String = String.Empty
t.Start()
For i As Integer = 1 To 10
szMsg &= "First : " & i & vbCrLf
Thread.Sleep(100)
Next
MessageBox.Show(szMsg)
End Sub
Sub DoTheTask()
Dim szMsg As String = String.Empty
For i As Integer = 1 To 10
szMsg &= "Second : " & i & vbCrLf
Thread.Sleep(200)
Next
MessageBox.Show(szMsg)
End Sub
Working with threads: Thread.Current.Thread returns the current thread, once we have a reference to it, we can start, suspend, resume or abort it (using methods of thread class). Thread is automatically terminated when it reaches exit or end, but can be suspended or aborted. Start, suspending or aborting of thread doesn’t happen right away, unless done for current thread.
Sub TestThread()
Dim t As New Thread(New ThreadStart(AddressOf DoTheTask))
'These work only with current thread
t.Start()
t.Suspend()
t.Resume()
t.Abort()
End Sub
Also, Timeout.Infinite can suspend till infinity or another thread invokes it.
Sub TestThread()
Dim t As New Thread(New ThreadStart(AddressOf DoTheTask))
t.Start()
t.Join()
End Sub
Join can take optional timeout, returns true if the method is declared with the given timespan, false if nto
Sub TestThread()
Dim t As New Thread(New ThreadStart(AddressOf DoTheTask))
t.Start()
Do Until t.Join(1000)
..
Loop
End Sub
Thread properties: IsAlive can be used to see if the thread is alive. A bit wise property is used to check the state of any thread. All threads have a name property, usually null, but can be used.
Abort The thread has been aborted
AbortRequested The thread is responding to an abort request
Background The thread is running in the background. (Same as the IsBackground property).
Running The thread is running. (Another thread has called the start method)
Stopped The thread has been stopped. (A hread can never leave this state.)
StopRequested The thread is about to stop.
Suspended The thread has been suspended.
SuspendedRequested The thread has been created, but the start method hasn’t been call yet.
WaitSleepJoin The thread has called Monitor.Wait or Thread.Join on another thread.
Debugging Thrads:.NET UI allows us to watch threads. It allows us to watch
(i) number fo logical threads
(ii) number of current physical threads
(iii) number of current recognized threads
(iv) Contention rated/second (reate at which threads in the runtime fail to acquire a managed lock.
(v) Total number of contentions, the number of times thread in the common language runtime have failed to acquire a managed lock.
(vi) Current queue/sec.
______________________________________________
*Notes from Balena's book VB.NET
______________________________________________
Add a comment