| | |
TryEnter always return true in Timer Event
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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);
}
}
}
}
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);
}
}
}
}
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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
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
![]() |
Similar Threads
- how do i add javascript to a form that i made that is all php? (JavaScript / DHTML / AJAX)
- I need a little help with my button processing (javascript) (JavaScript / DHTML / AJAX)
- Return True/False if Process is Running (C#)
- Capturing keystokes during program execution (Python)
- adding keylistener (Java)
- Re-running an onload event (JavaScript / DHTML / AJAX)
- dynamic html (PHP)
- Timer event doesn't get triggered (C++)
- Can you add pictures/sounds in a win32 console app? (C)
Other Threads in the C# Forum
- Previous Thread: changing colors of button
- Next Thread: How to change settings of SQL Logins using Visual C# code?
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development dll draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml





