Every now and then I'll write a program to remind myself of something and so here's one I was writing yesterday when something strange happened.
Original Source
using System;
using System.Threading;
namespace TestThreadFunctions
{
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main thread";
var t = JoiningAnAlreadyCompletedThread();
JoiningIsIdempotent(t);
Console.ReadKey();
}
static Thread JoiningAnAlreadyCompletedThread()
{
Thread t = new Thread(() =>
{
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I'm done now.\n");
})
{ Name = "Worker 1" };
t.Start();
// Give a long time-out so we know that the
// current thread did not wait the entire time-out
// or that it didn't wait at all because the thread
// we are joining on has already completed.
t.Join(10000);
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n");
return t;
}
static void JoiningIsIdempotent(Thread threadToJoinOn)
{
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I am about to join a thread that I already joined with earlier. I want to see if joining is idempotent.\n");
// Joining is idempotent
threadToJoinOn.Join();
Console.WriteLine($"{Thread.CurrentThread.Name} ({Thread.CurrentThread.ManagedThreadId}): I just finished joining with a thread that I already joined with earlier. See! nothing happened. Joining is idempotent.\n");
}
}
}
When I looked up the program in Reflector, it had this strange if
condition at the beginning of the JoiningAnAlreadyCompletedThread
method like so:
private static Thread JoiningAnAlreadyCompletedThread()
{
if (<>c.<>9__1_0 == null)
{
ThreadStart start1 = <>c.<>9__1_0;
}
...
}
I read it a couple of times just to confirm that I wasn't missing something. This is so strange it does nothing.
Just for the sake of completeness, here's the fully decompiled program from Reflector.
Reflector Decompiled Source
internal class Program
{
// Methods
private static Thread JoiningAnAlreadyCompletedThread()
{
if (<>c.<>9__1_0 == null)
{
ThreadStart start1 = <>c.<>9__1_0;
}
Thread thread1 = new Thread(<>c.<>9__1_0 = new ThreadStart(<>c.<>9.<JoiningAnAlreadyCompletedThread>b__1_0)) {
Name = "Worker 1"
};
Thread t = thread1;
t.Start();
t.Join(0x2710);
Console.WriteLine(string.Format("{0} ({1}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
return t;
}
private static void JoiningIsIdempotent(Thread threadToJoinOn)
{
Console.WriteLine(string.Format("{0} ({1}): I am about to join a thread that I already joined with earlier. I want to see if joining is idempotent.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
threadToJoinOn.Join();
Console.WriteLine(string.Format("{0} ({1}): I just finished joining with a thread that I already joined with earlier. See! nothing happened. Joining is idempotent.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
}
private static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main thread";
JoiningIsIdempotent(JoiningAnAlreadyCompletedThread());
Console.ReadKey();
}
// Nested Types
[Serializable, CompilerGenerated]
private sealed class <>c
{
// Fields
public static readonly Program.<>c <>9 = new Program.<>c();
public static ThreadStart <>9__1_0;
// Methods
internal void <JoiningAnAlreadyCompletedThread>b__1_0()
{
Console.WriteLine(string.Format("{0} ({1}): I'm done now.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
}
}
}
I looked up the same source in IL Spy and it didn't do this weird thing. It did not have this strange if
condition that did nothing.
IL Spy Decompiled
// TestThreadFunctions.Program
private static Thread JoiningAnAlreadyCompletedThread()
{
ThreadStart arg_20_0;
if ((arg_20_0 = Program.<>c.<>9__1_0) == null)
{
arg_20_0 = (Program.<>c.<>9__1_0 = new ThreadStart(Program.<>c.<>9.<JoiningAnAlreadyCompletedThread>b__1_0));
}
Thread thread = new Thread(arg_20_0)
{
Name = "Worker 1"
};
thread.Start();
thread.Join(10000);
Console.WriteLine(string.Format("{0} ({1}): I just joined on another thread that I knew was already completed even before I joined. See nothing happened. .NET doesn't punish you for joining a thread that has already completed, unlike POSIX systems, which do.\n", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
return thread;
}
So, I guess I don't know what the question here is. May be it is "why did Reflector do that?"
And then may be the answer is, "Well, decompilation is difficult," which I understand.
So, I really don't know what the question is but I guess I just wanted to share my bewilderment.
If you really see a question in here that has a plausible explanation, I'd love to know why Reflector did that.
Otherwise, I know this question is going to get downvoted and closed.