The following code strips a ThreadState to one of the four most useful values: Unstarted, Running,WaitSleepJoin, and Stopped:
public static ThreadState SimpleThreadState (ThreadState ts)
{
return ts & (ThreadState.Unstarted |
ThreadState.WaitSleepJoin |
ThreadState.Stopped);
}
I read the above in a book, but I am not quite sure what the author want to illustrate here. I have tested like below:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(SimpleThreadState(ThreadState.Aborted));
System.Console.WriteLine(SimpleThreadState(ThreadState.Background));
System.Console.WriteLine(SimpleThreadState(ThreadState.AbortRequested));
System.Console.WriteLine(SimpleThreadState(ThreadState.Suspended));
System.Console.WriteLine(SimpleThreadState(ThreadState.Unstarted));
System.Console.WriteLine(SimpleThreadState(ThreadState.WaitSleepJoin));
System.Console.WriteLine(SimpleThreadState(ThreadState.Stopped));
}
public static ThreadState SimpleThreadState(ThreadState ts)
{
return ts & (ThreadState.Unstarted |
ThreadState.WaitSleepJoin |
ThreadState.Stopped);
}
}
And here is the running result: Running Running Running Running Unstarted WaitSleepJoin Stopped
The last three lines of output is straight forward, but why all else outputs Running state?
Thanks!