I'm trying to study for a cert and I'm writing a few examples loosely based
on Chapter 7 of the 536 manual. I am not used to thread programming. The
manual states that: 'Any attempt to abort a thread while it is within a
critical
region will have to wait until after the critical region is complete.' So,
I wrote the following class, used in a Dot Net 2.0 console app:
Imports System.Threading
Public Class Class5
Dim operation As New ThreadStart(AddressOf AbortThisThread)
Dim newThread As New Thread(operation)
Public Sub New()
newThread.Start()
Thread.Sleep(500)
newThread.Abort()
Console.WriteLine("In New, after abort, press any key...")
Console.ReadKey()
End Sub
Shared Sub AbortThisThread()
Try
Thread.BeginCriticalRegion()
Console.WriteLine("Class5:Thread: {0} in AbortThisThread sub",
Thread.CurrentThread.ManagedThreadId)
Console.ReadKey()
Console.WriteLine("In AbortThisThread, after ReadKey")
Thread.EndCriticalRegion()
Catch ex As ThreadAbortException
Console.WriteLine("Exception in AbortThisThread, error = " +
ex.Message)
End Try
End Sub
End Class
The output is as follows:
Class5:Thread: 10 in AbortThisThread sub
In New, after abort, press any key...
Exception in AbortThisThread, error = Thread was being aborted.
So it looks to me like I was able to interupt the critical region.
Comments? Am I making any sense at all? (Or does use of I/O invalidate
this begin/end scheme?)