thread unstarted

  • Thread starter Thread starter Kovan Akrei
  • Start date Start date
K

Kovan Akrei

Hi,

When I check whether one my thread are running I use a bitmask like this:

(myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0
(got it from MSDN)

How would I check whether a thread has been started or not without using the
property IsAlive? I'm trying to use a bit mask like this;

if ((myThread.ThreadState | ThreadState.Unstarted) = = 8)
myThread.Start();

but I get an error message :
Operator '= =' cannot be applied to operands of type
'System.Threading.ThreadState' and 'int'.
I'm I doing something wrong? I can fix this by retyping av the bitmask to
(int), but I was wondering if there is another way to bitmask and get the
right result?

Regards from
Kovan
 
Hi,
[inline]

Kovan Akrei said:
Hi,

When I check whether one my thread are running I use a bitmask like this:

(myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0
(got it from MSDN)

How would I check whether a thread has been started or not without using the
property IsAlive? I'm trying to use a bit mask like this;

if ((myThread.ThreadState | ThreadState.Unstarted) = = 8)
myThread.Start();

What about :

if ( (myThread.ThreadState & ThreadState.Unstarted) ==
ThreadState.Unstarted)
myThread.Start();

For masking bits, use the &-operator.
but I get an error message :
Operator '= =' cannot be applied to operands of type
'System.Threading.ThreadState' and 'int'.
I'm I doing something wrong? I can fix this by retyping av the bitmask to
(int),

Don't know what you mean with 'av', but you can safely cast int to
threadstate or threadstate to int.

HTH,
greetings
 
if ((myThread.ThreadState | ThreadState.Unstarted) = = 8)
What about :

if ( (myThread.ThreadState & ThreadState.Unstarted) ==
ThreadState.Unstarted)
myThread.Start();
As far I see this check will return true for the following
myThread.ThreadState:

if myThread.ThreadState = = Unstarted ( = = 8 = = 00001000 (in bits))
and
if myThread.ThreadState = = Abort ( = = 256 = = 11111111 (in bits))

and try to start an aborted thread --> Exception . I want to be sure that
the thread I'm trying to start has not been started yet.
That's why I thought may be using | (bitwise or) to check the state of the
thread I want to start.

Regards from Kovan
 
Hi Kovan,

If you want to check whether some bit(s) are set you should use bitwise AND
operation (&).

So you can use
if ((myThread.ThreadState & ThreadState.Unstarted) = =
ThreadState.Unstarted)
myThread.Start();

or

if ((myThread.ThreadState & ThreadState.Unstarted) ! = 0)
myThread.Start();

Both gives you the same result. Bare in mind that the second works only if
ThreadState.Unstarted is only one bit. As long as ThreadState.Unstarted is
documented to be 8 (one bit) it is safe to use it
 
Hi Stoitcho,
If you want to check whether some bit(s) are set you should use bitwise AND
operation (&).

So you can use
if ((myThread.ThreadState & ThreadState.Unstarted) = =
ThreadState.Unstarted)
myThread.Start();
This is true, but what if myThread.ThreadState is in Aborted (256) then the
expression above (myThread.ThreadState & ThreadState.Unstarted) = =
ThreadState.Unstarted) return true too. Too avoid this I have added the
following expression (&& myThread.ThreadState != ThreadState.Aborted)
or

if ((myThread.ThreadState & ThreadState.Unstarted) ! = 0)
myThread.Start();

Both gives you the same result. Bare in mind that the second works only if
ThreadState.Unstarted is only one bit. As long as ThreadState.Unstarted is
documented to be 8 (one bit) it is safe to use it
One bit can not have a value of 8 does it? I thought a ThreadState is a
value between 0 and 256 (1 byte).

Kovan
--
HTH
B\rgds
100 [C# MVP]

Kovan Akrei said:
Hi,

When I check whether one my thread are running I use a bitmask like this:

(myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted))
==
0
(got it from MSDN)

How would I check whether a thread has been started or not without using the
property IsAlive? I'm trying to use a bit mask like this;

if ((myThread.ThreadState | ThreadState.Unstarted) = = 8)
myThread.Start();

but I get an error message :
Operator '= =' cannot be applied to operands of type
'System.Threading.ThreadState' and 'int'.
I'm I doing something wrong? I can fix this by retyping av the bitmask to
(int), but I was wondering if there is another way to bitmask and get the
right result?

Regards from
Kovan
 
Hi Kovan
This is true, but what if myThread.ThreadState is in Aborted (256) then the
expression above (myThread.ThreadState & ThreadState.Unstarted) = =
following expression (&& myThread.ThreadState != ThreadState.Aborted)

256 = 1 0000 0000

only one bit set. Hence, the above will work
One bit can not have a value of 8 does it? I thought a ThreadState is a
value between 0 and 256 (1 byte). The range of bytes are 0..255 (bits) 256
has 9 bits

What I ment is that the flag has one bit set. 8 = 00001000

BTW the unerlying type for ThreadState is *int* not byte (you cannot fit 256
in one byte)
 
Hi,
[inline]
Kovan Akrei said:
As far I see this check will return true for the following
myThread.ThreadState:

if myThread.ThreadState = = Unstarted ( = = 8 = = 00001000 (in bits))
and
if myThread.ThreadState = = Abort ( = = 256 = = 11111111 (in bits))

Like Stoicho already said, ThreadState.Aborted isn't 1111 1111 it's 1 0000
0000.
(1 0000 0000 AND 0000 1000) != 0000 1000.
So it won't be true for ThreadState.Aborted.

HTH,
greetings
 
BMermuys said:
Hi,
[inline]
Kovan Akrei said:
As far I see this check will return true for the following
myThread.ThreadState:

if myThread.ThreadState = = Unstarted ( = = 8 = = 00001000 (in bits))
and
if myThread.ThreadState = = Abort ( = = 256 = = 11111111 (in bits))

Like Stoicho already said, ThreadState.Aborted isn't 1111 1111 it's 1 0000
0000.
(1 0000 0000 AND 0000 1000) != 0000 1000.
So it won't be true for ThreadState.Aborted.

Ok. I see. I thought ThreadState was only 8 bits long. My fault. Thank you
all for your help.

Best regards from
Kovan
 
Ofcourse. you are right. My mistake. I thought may be ThreadState values
where from 0-255 (8 bits). But it is an int. Thanks for your help.

Best regards from
Kovan

Stoitcho Goutsev (100) said:
Hi Kovan
This is true, but what if myThread.ThreadState is in Aborted (256) then the
expression above (myThread.ThreadState & ThreadState.Unstarted) = = the
following expression (&& myThread.ThreadState != ThreadState.Aborted)

256 = 1 0000 0000

only one bit set. Hence, the above will work
only
ThreadState.Unstarted
is
One bit can not have a value of 8 does it? I thought a ThreadState is a
value between 0 and 256 (1 byte). The range of bytes are 0..255 (bits)
256
has 9 bits

What I ment is that the flag has one bit set. 8 = 00001000

BTW the unerlying type for ThreadState is *int* not byte (you cannot fit 256
in one byte)
 
Back
Top