Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:40:13+00:00 2026-05-22T01:40:13+00:00

The following code strips a ThreadState to one of the four most useful values:

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:40:14+00:00Added an answer on May 22, 2026 at 1:40 am

    Bascially this is a binary mask. By or’ing together

    ThreadState.Unstarted, ThreadState.WaitSleepJoin,ThreadState.Stopped you construct a mask. When you and that mask with another value tells if you an of the other bits are still turned on.

    Let’s pretend that we’re dealing with a four bit int Thread.Unstarted == 1, ThreadState.WaitSleepJoing==2, ThreadState.Stopped==4, ThreadState.Aborted= 8 and ThreadState.Running = 0 . These are not the real values, just using them to make the example easier. Look at ThreadState for the real values.

    So in binary:
    Thread.Unstarted == 0001
    Thread.WaitSleepJoing == 0010
    Thread.Stopped == 0100

    When you or these values together you get 0111.

    Now when we and 0111 with ThreadState.Aborted (1000) you get 0000 which is ThreadState.Running.

    So what the author is basically trying to show you here is that ThreadState is a bit vector and you have to construct a mask to see which values are set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the following code and it still strips out all the
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
I am currently running the following code based on Chapter 12.5 of the Python
The following Perl one-liner works as I expect it to; it strips my file
Hi I have a following code below: public Date convertFromGMTToLocal(Date date) { return new
I'm transforming xml using the following code. It works fine for one xslt but
I've got the following code, and need to strip all non alpha numeric characters.
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
Following code iterates through many data-rows, calcs some score per row and then sorts
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.