This should exit properly.
I ran code through ildasm that had a lock
statement in it. If you look carefully at the IL code
the lock is transformed into a try/finally block.
So any exiting should trigger the finally block
CODE
// Class with lock
public class Alpha
{
public void Beta()
{
lock (this)
{
while (true)
Console.WriteLine("Alpha.Beta is runnin in its own thread");
}
}
};
CODE
// IL Code
.method public hidebysig instance void Beta() cil managed
{
// Code size 36 (0x24)
.maxstack 2
.locals init ([0] class LibTester.learnThreading.Alpha CS$2$0000,
[1] bool CS$4$0001)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: dup
IL_0003: stloc.0
IL_0004: call void [mscorlib]System.Threading.Monitor::Enter(object)
IL_0009: nop
.try
{
IL_000a: nop
IL_000b: br.s IL_0018
IL_000d: ldstr "Alpha.Beta is runnin in its own thread"
IL_0012: call void [mscorlib]System.Console::WriteLine(string)
IL_0017: nop
IL_0018: ldc.i4.1
IL_0019: stloc.1
IL_001a: br.s IL_000d
} // end .try
finally
{
IL_001c: ldloc.0
IL_001d: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_0022: nop
IL_0023: endfinally
} // end handler
} // end of method Alpha::Beta