Access to Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikolai Sander

    #1

    Access to Thread

    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.TryEnte r() from a different thread fails.

    Any feedback appreciated.

    Nikolai


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Access to Thread

    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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Nikolai Sander" <ns@nospam.ne t> wrote in message
    news:eTXYSFyjDH A.2424@TK2MSFTN GP10.phx.gbl...[color=blue]
    > 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.TryEnte r() from a different thread fails.
    >
    > Any feedback appreciated.
    >
    > Nikolai
    >
    >[/color]


    Comment

    • 100

      #3
      Re: Access to Thread

      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 Sander" <ns@nospam.ne t> wrote in message
      news:eTXYSFyjDH A.2424@TK2MSFTN GP10.phx.gbl...[color=blue]
      > 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.TryEnte r() from a different thread fails.
      >
      > Any feedback appreciated.
      >
      > Nikolai
      >
      >[/color]


      Comment

      • Nikolai Sander

        #4
        Re: Access to Thread


        [color=blue]
        >Nikolai,
        >
        > I am curious, why do you want to do this?[/color]

        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

        Comment

        • Nikolai Sander

          #5
          Re: Access to Thread

          On Fri, 10 Oct 2003 11:16:52 -0400, "100" <100@100.com> wrote:
          [color=blue]
          >Hi Nikolai,
          >What do you mean by access the thread?[/color]

          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

          Comment

          Working...