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
  1. Home
  2. General Programming
  3. C#
  4. TryEnter always return true in Timer Event

TryEnter always return true in Timer Event

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 4 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.
  • G Offline
    G Offline
    geossl
    wrote on last edited by
    #1

    Dear All, That's strange that when I use TryEnter in Timer event which triggers every 1 sec, the TryEnter() always returns true. Why there is such an error? public partial class Form1 : Form { private object BlockingObj = new object(); private void timer1_Tick(object sender, EventArgs e) { bool ret = System.Threading.Monitor.TryEnter(BlockingObj, 1); if (ret){ try { DialogResult result = MessageBox.Show("test", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); } finally { System.Threading.Monitor.Exit(BlockingObj); } } } }

    N OriginalGriffO N 3 Replies Last reply
    0
    • G geossl

      Dear All, That's strange that when I use TryEnter in Timer event which triggers every 1 sec, the TryEnter() always returns true. Why there is such an error? public partial class Form1 : Form { private object BlockingObj = new object(); private void timer1_Tick(object sender, EventArgs e) { bool ret = System.Threading.Monitor.TryEnter(BlockingObj, 1); if (ret){ try { DialogResult result = MessageBox.Show("test", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); } finally { System.Threading.Monitor.Exit(BlockingObj); } } } }

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      geossl wrote:

      the TryEnter() always returns true. Why there is such an error?

      Which means it is able to get a lock on the supplied object. You are releasing that lock in finally block as well. So next time when timer ticks, object will be available for locking again. I think TryEnter is behaving as expected.

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • G geossl

        Dear All, That's strange that when I use TryEnter in Timer event which triggers every 1 sec, the TryEnter() always returns true. Why there is such an error? public partial class Form1 : Form { private object BlockingObj = new object(); private void timer1_Tick(object sender, EventArgs e) { bool ret = System.Threading.Monitor.TryEnter(BlockingObj, 1); if (ret){ try { DialogResult result = MessageBox.Show("test", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); } finally { System.Threading.Monitor.Exit(BlockingObj); } } } }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        There are a couple of things here: 1) Please try to use the "code block" button below to enclose your samples - it preserves the formatting and make it easier to read.

        public partial class Form1 : Form
            {
            private object BlockingObj = new object();
            private void timer1\_Tick(object sender, EventArgs e)
                {
                bool ret = System.Threading.Monitor.TryEnter(BlockingObj, 1);
                if (ret)
                    {
                    try
                        {
                        DialogResult result = MessageBox.Show("test", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        }
                    finally
                        {
                        System.Threading.Monitor.Exit(BlockingObj);
                        }
                    }
                }
            }
        
        1. The code: TryEnter attempts to establish an exclusive lock on the object. If it suceeds, then you display a messagebox (which is application modal, so the App can do nothing else until it is dismissed) then release the lock. Comments: A) because you release the lock in the timer event in which you establish it, it will always succeed. B) Why keep the result of a MessageBox if you are going to ignore it? C) Under what circumstances do you expect MessageBox to throw an exception? By all means put the try...catch around the exclusive lock, that will catch null objects, but otherwise it just complicates your code unneccessarily.

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • G geossl

          Dear All, That's strange that when I use TryEnter in Timer event which triggers every 1 sec, the TryEnter() always returns true. Why there is such an error? public partial class Form1 : Form { private object BlockingObj = new object(); private void timer1_Tick(object sender, EventArgs e) { bool ret = System.Threading.Monitor.TryEnter(BlockingObj, 1); if (ret){ try { DialogResult result = MessageBox.Show("test", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); } finally { System.Threading.Monitor.Exit(BlockingObj); } } } }

          N Offline
          N Offline
          Nicholas Butler
          wrote on last edited by
          #4

          TryEnter always succeeds because you are always calling it on the same ( UI ) thread and Monitor is reentrant. Your MessageBox doesn't totally block the UI thread. It runs a partial message loop and one of the messages it processes is the Windows.Forms.Timer message. Therefore, when the next tick happens, your handler is run again from inside the MessageBox message loop. TryEnter succeeds because the UI thread already owns the lock and Monitor allows a thread to lock an object multiple times. Nick

          ---------------------------------- Be excellent to each other :)

          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