TryEnter always return true in Timer Event
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);
}
}
}
}
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

zirano latt20 reputationPosted May 12, 2009, 11:51 PM EDT
bool ret = System.Threading.Monitor.TryEnter(BlockingObj, 1); // 1millisecond to acquire the lock.