If you try to change the background status of a
thread in the thread pool via the IsBackground property on the current thread, you??™ll find that
it has no effect.
nNote It??™s important to realize that when your asynchronous code is executing and when the callback is
executing, you are running in an arbitrary thread context. You cannot make any assumptions about which
thread is running your code.
Timers
Yet another entry point into the thread pool is via Timer objects in the System.Threading namespace.
As the name implies, you can arrange for the thread pool to call a delegate at a specific
time as well as at regular intervals. Let??™s look at an example of how to use the Timer object:
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading
Public Class EntryPoint
Private Shared Sub TimerProc(ByVal state As Object)
Console.WriteLine("The current time is {0} on thread {1}", _
DateTime.Now, Thread.CurrentThread.GetHashCode())
Thread.Sleep(3000)
End Sub
Shared Sub Main()
Console.WriteLine("Press
when finished" & _
Constants.vbCrLf)
Dim myTimer As Timer = _
New Timer(New TimerCallback( _
AddressOf EntryPoint.TimerProc), _
Nothing, 0, 2000)
CHAPTER 13 n THREADING 315
Console.ReadLine()
myTimer.Dispose()
End Sub
End Class
When the timer is created, you must give it a delegate to call at the required time.
Pages:
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511