TryEnter always return true in Timer Event

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 3
Reputation: geossl is an unknown quantity at this point 
Solved Threads: 0
geossl geossl is offline Offline
Newbie Poster

TryEnter always return true in Timer Event

 
0
  #1
May 13th, 2009
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);
}
}


}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: TryEnter always return true in Timer Event

 
0
  #2
May 13th, 2009
Why do you think this is an error.
Your timer is on a 1000 ms loop, and the TryEnter has a max wait time of 1ms. It should always return true, because there was nothing preventing it (IOW nothing else locking the object for more than 1001ms.

Try having another method or timer that holds the lock for more than one second while the 1second timer is active, and see if it reacts the way you think it should.

// Jerry
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC