P
pistmaster
Hi,
I have a timer which I use to process some events that have been added
to a queue. The processing may take longer than the timer tick and so
the timer catches up with itself. I would like to lock the timer so
that this doesn't happen, so I do:
timer_Tick()
{
if (System.Threading.Monitor.TryEnter(queue.SyncRoot))
{
try {
// process queue
} finally
{
System.Threading.Monitor.Exit(queue.SyncRoot);
}
}
}
I use TryEnter because I don't want to exhaust the thread pool but it
makes no difference. The trouble is the function doesn't actually
lock, and always processes the queue regardless. Is this because
locks just don't work in timers? Something to do with the context
being the same, like with you do:
function1()
{
lock(a)
{
function2();
}
}
function2()
{
lock(a)
{
}
}
I have a timer which I use to process some events that have been added
to a queue. The processing may take longer than the timer tick and so
the timer catches up with itself. I would like to lock the timer so
that this doesn't happen, so I do:
timer_Tick()
{
if (System.Threading.Monitor.TryEnter(queue.SyncRoot))
{
try {
// process queue
} finally
{
System.Threading.Monitor.Exit(queue.SyncRoot);
}
}
}
I use TryEnter because I don't want to exhaust the thread pool but it
makes no difference. The trouble is the function doesn't actually
lock, and always processes the queue regardless. Is this because
locks just don't work in timers? Something to do with the context
being the same, like with you do:
function1()
{
lock(a)
{
function2();
}
}
function2()
{
lock(a)
{
}
}