473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

timers and locks

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)
{
}
}

Jun 22 '07 #1
3 2114
It is not the timer that does the processing; it is the timer's Elapsed
event handler. The timer's Elapsed event handler can check to see if the
processing is still proceeding, and pause the timer until it has finished.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<pi********@googlemail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
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)
{
}
}

Jun 22 '07 #2
Hi,

what sort of timer do you use. There are three timer classes in the
..NET-Framework, for different purposes.
You can use intellisense to see the full namspace of the timer class.

The System.Windows.Forms.Timer e.g. processes all events in the GUI-Thread.
So there would be no locking needed. Atleast not against oter timer events.

Christof

<pi********@googlemail.comschrieb im Newsbeitrag
news:11**********************@o11g2000prd.googlegr oups.com...
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)
{
}
}

Jun 22 '07 #3

"Christof Nordiek" <cn@nospam.deschrieb im Newsbeitrag
news:Om**************@TK2MSFTNGP02.phx.gbl...
Hi,

what sort of timer do you use. There are three timer classes in the
.NET-Framework, for different purposes.
You can use intellisense to see the full namspace of the timer class.

The System.Windows.Forms.Timer e.g. processes all events in the
GUI-Thread. So there would be no locking needed. Atleast not against oter
timer events.

Christof
Seems to be the System.Wndows.Forms-Timer. This is the only one that has a
Tick-Event.
So, as I said, the Tick-Calls can't be parallel anyway and there will be no
locking between them.

But, if the processing is really so lengthy, that a tick could come, before
the preceeding call is processed, you shouldn't doe it in the GUI-Thread
anyway. This will freeze your GUI.

Christof
Jun 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dmitry Demchuk | last post by:
Hi everybody. Recently I ran into situation with System.Threading.Timer in my ASP.NET application. I reinstalled Windows on one of my servers and got timers stop firing events after while, they...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
1
by: | last post by:
Frustrated.. (I have seen other posts regarding this problem with no resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually 2 boxes), have even run the service on WinXP SP2...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
5
by: FredC | last post by:
I have an application that uses mulitple timers. Each of the timer event handlers manipulate a common array of data. I'm getting Null refererance errors - should I put a lock on the array when I...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
2
by: Rob | last post by:
I've got the following skeleton in my HttpApplication (global.aspx) file: Public Sub New() MyBase.New() ApplicationTimer = New System.Timers.Timer ApplicationTimer.BeginInit()...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
4
by: Blue Streak | last post by:
Hi, Folks! I have a question about concurrent timers. In my current project I am writing an NT Service (I know that is old terminology but I work with idiots who think you mean Web Service when...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.