On Tue, 18 Jun 2002 07:32:45 +0200, Alois Reisinger <a.reisinger@GBS-
SOLUTIONS.AT> wrote:

>Is there any chance to find out if two threads are inside the same part of
code?

Yes, why not?

Consider you have a critical part of code which is not locking on any
object. Then you can use interlocked counters to register threads
entering/exiting this place of concern. But this only registers the
problem, it does not solve it!

Otherwise if you use locks to protect the critical code, (which solves the
problem on first place) you can exted that locking to be done through the
Monitor class and by using Monitor.TryEnter(theSyncRootObject) insted of
lock(theSyncRootObject) you can be even called back/notified by the thread
who's trying to enter the critical section of the code while another thread
is still running inside that section.

Two more things about threads state just crossed my mind:

First, event if you interlock all places you play with the thread's state,
the thread can change it's state spontaneously. One reason for this to
happen is a unhandled exception in the thread's main function, which makes
the thread to finish (change it's state) without you knowing anything about
it.

Another one is that ThreadState enum has the [Flags] attribute and a thread
can be in more than one state at a single point in time. Remember your code
sample, the check there confirmed the thread is in Suspended state but it
does not say it is not in another state too, which second state dos not
allow resuming it. I find this a bit tricky to handle.

stef

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to