I have such code in my app:
var t = new Thread(new ThreadStart(new Action(SomeClass.SomeMethod)));
t.Start();
...
t.Join();
but as I understand, compiler does some optimization and run SomeMethod in the same thread
as main code. I check this by setting different Names of the thread in t and Thread.CurrentThread. How I can create and run thread to be sure it’s new thread.
As I understand it, if you tell a program to run a task in a new thread, that’s exactly what will happen. Those two threads may end up running on the same core (because your calling thread may not do much except wait for the other thread to complete), but you will have two different memory spaces and execution pointers in your process.
An easy way to prove it is to open up task manager, set a breakpoint in SomeMethod, check the thread count of VS (devenv.exe) in the Processes tab, then hit the Debug button to launch your program. When it hits the breakpoint, examine the thread count again; you’ll have two new threads, one for the main program execution flow and one for your worker thread. There may be one more for the attached debugger.