The compiler will not optimize your code in that manner. The code will be run in a new .NET thread. There must have been an error in the way you made your observations.
Note that .NET threads aren't necessarily equivalent to OS threads:
An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the CLR Hosting API to schedule many managed threads against the same operating system thread, or to move a managed thread between different operating system threads.
You could in theory see two managed threads with the same Windows thread ID, but the Thread.Name
property will still be different (assuming you initally set the names to two different values).
If you could post the code you used where you observed something being run in the wrong thread it might be possible to discover what error you have made.