Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. lock() and Thread.Interrupt()

lock() and Thread.Interrupt()

Scheduled Pinned Locked Moved C#
dotnetquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    Hi, My worker thread looks a bit like this:-

    private void CacheThread()
        {
            while (true)
            {
               lock (myobject)
               {
                   myobject.DoSomeWork();
               
               }
    
                try
                {
                    System.Threading.Thread.Sleep(100);
                }
                catch (ThreadInterruptedException)
                {
                }
            }
        }
    

    The thread spends the majority of it's time sleeping. Another thread uses Thread.Interrupt() to wake it up when it is necessary to DoSomeWork(). The majority of the time, it's fine - the thread is in the sleep and is interrupted. But sometimes, I get an unhandled ThreadInterruptedException on the lock() line. I presume that internally, CLR is using a win32 WaitFor... function, and is interupted. But I don't want my thread to wake up until it can claim the lock. Is there an alternative? I want to wake the thread from my sleep, but not from a lock()-induced sleep. Thanks Jon

    using System.Beer;

    G A J L 4 Replies Last reply
    0
    • J Jon Hulatt

      Hi, My worker thread looks a bit like this:-

      private void CacheThread()
          {
              while (true)
              {
                 lock (myobject)
                 {
                     myobject.DoSomeWork();
                 
                 }
      
                  try
                  {
                      System.Threading.Thread.Sleep(100);
                  }
                  catch (ThreadInterruptedException)
                  {
                  }
              }
          }
      

      The thread spends the majority of it's time sleeping. Another thread uses Thread.Interrupt() to wake it up when it is necessary to DoSomeWork(). The majority of the time, it's fine - the thread is in the sleep and is interrupted. But sometimes, I get an unhandled ThreadInterruptedException on the lock() line. I presume that internally, CLR is using a win32 WaitFor... function, and is interupted. But I don't want my thread to wake up until it can claim the lock. Is there an alternative? I want to wake the thread from my sleep, but not from a lock()-induced sleep. Thanks Jon

      using System.Beer;

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You get the exception because you are trying to interrupt the thread while it's not sleeping. If you want to handle that, you will have to catch the exception while the thread is working too. -- modified at 7:02 Tuesday 11th July, 2006 On second thought, that could be a bit messy. A better way would be to add a flag to the class that you set while the thread goes into sleep mode, and a property that returns the flag so that you can use it to determine if the thread is sleeping or not, so that you don't interrupt it while it's not sleeping. --- b { font-weight: normal; }

      J 1 Reply Last reply
      0
      • G Guffa

        You get the exception because you are trying to interrupt the thread while it's not sleeping. If you want to handle that, you will have to catch the exception while the thread is working too. -- modified at 7:02 Tuesday 11th July, 2006 On second thought, that could be a bit messy. A better way would be to add a flag to the class that you set while the thread goes into sleep mode, and a property that returns the flag so that you can use it to determine if the thread is sleeping or not, so that you don't interrupt it while it's not sleeping. --- b { font-weight: normal; }

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        Not true. If you look at MSDN for Thread.Interrupt(), it states:

        If this thread is not currently blocked in a wait, sleep, or join state, it will be interrupted when it next begins to block.

        ThreadInterruptedException is thrown in the interrupted thread, but not until the thread blocks. If the thread never blocks, the exception is never thrown, and thus the thread might complete without ever being interrupted.

        using System.Beer;

        1 Reply Last reply
        0
        • J Jon Hulatt

          Hi, My worker thread looks a bit like this:-

          private void CacheThread()
              {
                  while (true)
                  {
                     lock (myobject)
                     {
                         myobject.DoSomeWork();
                     
                     }
          
                      try
                      {
                          System.Threading.Thread.Sleep(100);
                      }
                      catch (ThreadInterruptedException)
                      {
                      }
                  }
              }
          

          The thread spends the majority of it's time sleeping. Another thread uses Thread.Interrupt() to wake it up when it is necessary to DoSomeWork(). The majority of the time, it's fine - the thread is in the sleep and is interrupted. But sometimes, I get an unhandled ThreadInterruptedException on the lock() line. I presume that internally, CLR is using a win32 WaitFor... function, and is interupted. But I don't want my thread to wake up until it can claim the lock. Is there an alternative? I want to wake the thread from my sleep, but not from a lock()-induced sleep. Thanks Jon

          using System.Beer;

          A Offline
          A Offline
          Aaron Dilliard
          wrote on last edited by
          #4

          Just looking at your code (and I could be misunderstanding what you are trying to do here), but you are calling Thread.Sleep with a timeout of 100ms, which means if you dont interrupt it within 100ms of calling, then it wakes back up and resumes the code. If you interrupt the thread while it is waiting to aquire a lock then you will certainly get an exception (in this case unhandled). Perhaps specify an infinite timeout in the Sleep() method? That way it will stay in "stay asleep" until explicitly interrupted. Aaron

          1 Reply Last reply
          0
          • J Jon Hulatt

            Hi, My worker thread looks a bit like this:-

            private void CacheThread()
                {
                    while (true)
                    {
                       lock (myobject)
                       {
                           myobject.DoSomeWork();
                       
                       }
            
                        try
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                        catch (ThreadInterruptedException)
                        {
                        }
                    }
                }
            

            The thread spends the majority of it's time sleeping. Another thread uses Thread.Interrupt() to wake it up when it is necessary to DoSomeWork(). The majority of the time, it's fine - the thread is in the sleep and is interrupted. But sometimes, I get an unhandled ThreadInterruptedException on the lock() line. I presume that internally, CLR is using a win32 WaitFor... function, and is interupted. But I don't want my thread to wake up until it can claim the lock. Is there an alternative? I want to wake the thread from my sleep, but not from a lock()-induced sleep. Thanks Jon

            using System.Beer;

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            Hi Jon Wouldn't it be easier to use an System.Threading.AutoResetEvent? This way you don't have to sleep every loop, but rather, simply call the AutoResetEvent instance's WaitOne method. This also alleviates the need for the CLR to throw a ThreadInterruptedException; throwing exceptions is never very performant. Your resulting code would look something like:

            void CacheThread(AutoResetEvent notifier)
            {
            while(true)
            {
            notifier.WaitOne(); // this will block until notifier's Set method has been called (assumingly by some other thread).
            lock(myobject)
            {
            myobject.DoSomeWorker();
            }
            }
            }

            Unless I'm misunderstanding what you're trying to do, this should suffice for you and is a lot cleaner, easier, and would perform better than your equivalent sleep-while-listening-for-ThreadInterruptException.

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            1 Reply Last reply
            0
            • J Jon Hulatt

              Hi, My worker thread looks a bit like this:-

              private void CacheThread()
                  {
                      while (true)
                      {
                         lock (myobject)
                         {
                             myobject.DoSomeWork();
                         
                         }
              
                          try
                          {
                              System.Threading.Thread.Sleep(100);
                          }
                          catch (ThreadInterruptedException)
                          {
                          }
                      }
                  }
              

              The thread spends the majority of it's time sleeping. Another thread uses Thread.Interrupt() to wake it up when it is necessary to DoSomeWork(). The majority of the time, it's fine - the thread is in the sleep and is interrupted. But sometimes, I get an unhandled ThreadInterruptedException on the lock() line. I presume that internally, CLR is using a win32 WaitFor... function, and is interupted. But I don't want my thread to wake up until it can claim the lock. Is there an alternative? I want to wake the thread from my sleep, but not from a lock()-induced sleep. Thanks Jon

              using System.Beer;

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Jon Hulatt wrote:

              I want to wake the thread from my sleep, but not from a lock()-induced sleep.

              I'm lost, lock is for suspending the thread until you signal the object. When you introduce Sleep also you then have two suspending mechanisms where only one should be needed. Also while(true) is usually NOT a good design. "true" should be replaced by something that will actually allow you to gracefully exit the thread.

              "Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?"
              Colin Angus Mackay in the C# forum

              led mike

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups