In my application I want my main thread to be not used by anything else. I have to do some parallel processing that I would like to be done by different threads. For that I am using Parallel.For as follows
static void SomeMethod()
{
Console.WriteLine(string.Format("Main Thread ID before parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId));
Parallel.For(0, 10, i =>
{
Console.WriteLine(string.Format("Output ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId));
});
Thread.Sleep(100);
Console.WriteLine(string.Format("Main Thread ID after parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId));
}
As you can see from the output main thread is using ThreadID 1 and some threads from Parallel.For are also using same thread.
Main Thread ID before parallel loop ->>>>>>> 1
Output ->>>>>>> 1
Output ->>>>>>> 1
Output ->>>>>>> 3
Output ->>>>>>> 4
Output ->>>>>>> 4
Output ->>>>>>> 4
Output ->>>>>>> 4
Output ->>>>>>> 5
Output ->>>>>>> 3
Output ->>>>>>> 1
Main Thread ID after parallel loop ->>>>>>> 1
Is there some way to make sure that anything in Parallel.For always run on separate thread so that main thread is always free.