Access to Thread

  • Thread starter Thread starter Nikolai Sander
  • Start date Start date
N

Nikolai Sander

Is there a way to get access to the Thread that owns a lock on an object
that was accuired with Monitor.Enter()? I want to access that thread when
the Monitor.TryEnter() from a different thread fails.

Any feedback appreciated.

Nikolai
 
Nikolai,

I am curious, why do you want to do this? The whole point of a lock (on
a very high level) is to keep the other threads out of that section while it
is running.

You can do this, but you have to set up some sort of interthread
communication mechanism. Basically, you send a message to the other thread
(while it is in the locked section) which will read the message and exit the
lock. Typically, this is done with some sort of looping mechanism which
checks a shared value between the two threads.

Hope this helps.
 
Hi Nikolai,
What do you mean by access the thread? Threads usually has nothing to access
except few properties which code executed in different thread might be
interested in. The actual data is located in objects which are accessible
from any thread in the application domain.

B\rgds
100
 
Nikolai,

I am curious, why do you want to do this?

I basically want to tell the thread that owns the lock to terminate
what it is doing in a controlled manner. I'm using exactly what you're
suggesting below (sending a message to the other thread etc.)

However the problem is, that I can't get hold of the thread that owns
the lock.

Any idea how I can get hold of the thread?

Note that I have multiple threads (2-4) that could own the lock and I
have another thread that checks if it can lock the object with
TryEnter and if it can't I want to abort the thread that currently
holds the lock.

Thanks

Nikolai
 
Hi Nikolai,
What do you mean by access the thread?

I mean the Thread object, so that I can access it's local storage, or
use it in a hashtable that manages my threads.

Thanks,

Nikolai
 
Back
Top