473,396 Members | 2,033 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,396 software developers and data experts.

Lock problems with System.Timers.Timer

Ben
Hello everybody

I got confused by this problem for which I don't have a logical explanation.
There is a Thread (ThreadA) which receives Events from another system thread
(ThreadS). ThreadA then adds a time stamp to the received event and adds it
to a event queue. This works well (therfore not shown here). The queue fills
up.
Then I used a timer to check every millisecond for new events in the queue
and send it to another thread (ThreadC). I included a lock statement on a
static object to ensure a critical section. But after some time (6 hours)
the timer stops.
I debugged it and found that i could even not restart the timer executing
m_EventTimer.Start().

What do I wrong? Why is the timer blocking?
Ok, perhaps, many events will be generated during the time the Timer_Elapsed
event happens. But the lock statement should do the job and not allow any
false Start() Stop() of the timer... What happens to the timer events which
have to wait? Does this anybody know?

Many thanks for tips
Ben

private void OnEventTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
lock(m_EventTimerLockObject)
{
MyClass.personalMessage tmpPersMsg;
try
{
m_EventTimer.Stop();
//send all events which already occurred to the TAPI class for logging
while(m_Events.Count > 0)
{
lock(m_Events)
{
tmpPersMsg= (MyClass.personalMessage )m_Events[0];
m_Events.RemoveAt(0);
}
this.OnLineMessage(new PersonalMessageEventArgs(tmpPersMsg));
}
}
catch(Exception ex)
{
Logging.LogDatabase(150103, ex.Message, ex.StackTrace,
LogSeverityDB.Error, "PM.cs"); // Event timer raised an exception.
}
finally
{
m_EventTimer.Start();
}
}
}
Alternative (right now in test):
private void OnEventTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if(System.Threading.Monitor.TryEnter(m_EventTimerL ockObject))
{
MyClass.personalMessage tmpPersMsg;
try
{
m_EventTimer.Stop();
//send all events which already occurred to the TAPI class for logging
while(m_Events.Count > 0)
{
lock(m_Events)
{
tmpPersMsg= (MyClass.personalMessage )m_Events[0];
m_Events.RemoveAt(0);
}
this.OnLineMessage(new PersonalMessageEventArgs(tmpPersMsg));
}
}
catch(Exception ex)
{
Logging.LogDatabase(150103, ex.Message, ex.StackTrace,
LogSeverityDB.Error, "PM.cs"); // Event timer raised an exception.
}
finally
{
System.Threading.Monitor.Exit(m_EventTimerLockObje ct);
m_EventTimer.Start();
}
}
}
May 11 '06 #1
4 5323
Hi,
"Ben" <Po************@kjkjkjkjkjkjkjkjkjkjkjkjkjkjkj.com > wrote in message
news:uu**************@TK2MSFTNGP02.phx.gbl...
Hello everybody

I got confused by this problem for which I don't have a logical
explanation.
There is a Thread (ThreadA) which receives Events from another system
thread (ThreadS).
How you assure than the event generated in thead S is handled in thread A ?
Then I used a timer to check every millisecond for new events in the queue
and send it to another thread (ThreadC).
In what thread is this?
I included a lock statement on a static object to ensure a critical
section. But after some time (6 hours) the timer stops.
I debugged it and found that i could even not restart the timer executing
m_EventTimer.Start().

What do I wrong? Why is the timer blocking?


I'm not very sure that you need all this, first of all why you need so many
threads doing part of the process?

In addition you don't have to use lock , simply use a Syncronized queue
(Queue.Syncronized ). then have one thread receive the message, prepare it
and place it in the queue.

Another thread will poll the queue at a regular interval and do any process.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 11 '06 #2
"Ben" <Po************@kjkjkjkjkjkjkjkjkjkjkjkjkjkjkj.com > wrote:
I got confused by this problem for which I don't have a logical explanation.
There is a Thread (ThreadA) which receives Events from another system thread
(ThreadS). ThreadA then adds a time stamp to the received event and adds it
to a event queue. This works well (therfore not shown here). The queue fills
up.
Then I used a timer to check every millisecond for new events in the queue
and send it to another thread (ThreadC). I included a lock statement on a
static object to ensure a critical section. But after some time (6 hours)
the timer stops.
I debugged it and found that i could even not restart the timer executing
m_EventTimer.Start().


First, System.Timers.Timer is a component for UIs, you should check out
System.Threading.Timer for working with threading.

However, you are designing your architecture around polling - you
shouldn't do that. Google "producer consumer queue", I think you should
design your architecture in a different way.
-- Barry
May 11 '06 #3
Ben

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uu**************@TK2MSFTNGP02.phx.gbl...
Hi,
"Ben" <Po************@kjkjkjkjkjkjkjkjkjkjkjkjkjkjkj.com > wrote in message
news:uu**************@TK2MSFTNGP02.phx.gbl...
Hello everybody

I got confused by this problem for which I don't have a logical
explanation.
There is a Thread (ThreadA) which receives Events from another system
thread (ThreadS).
How you assure than the event generated in thead S is handled in thread A
?


I checked it with the debugger. Breakpoints in the event receiving method
will be hit. It works and adds events to the queue. After some time I have
several thousand events in the queue which won't be processed because the
timer stops removing them from the queue. But here is the code in ThreadA
from the loop waiting for events from ThreadS:

// Get a event
Status = WaitForSingleObject(m_EventHandle, 200);
switch(Status)
{
case WaitForObjectsResults.WAIT_OBJECT_0: //a event is pending,
lineMsg = new Tapi.linemessage(0, 0, IntPtr.Zero, IntPtr.Zero,
IntPtr.Zero, IntPtr.Zero);
if (GetTapiMessage(ref lineMsg) ==
Tapi2Lib.LineErrReturn.LINEERR_OK)
{
//Create Event
if(m_EventThreadIsSendingLineMessages)
{
AddEvent(lineMsg);
}
}
else
{
//No Msg (LINEERR_OPERATIONFAILED) or error
}
break;
case WaitForObjectsResults.WAIT_TIMEOUT: //Nothing to do
break;
default: //WaitForObjectsResults.WAIT_FAILED,
WaitForObjectsResults.WAIT_ABANDONED
//TODO : GetLastError();
break;
}

private void AddEvent(MyClass.personalMessage tmpPersMsg)
{
lock(m_Events)
{
m_Events.Insert(m_Events.Count, lineMsg);
}
}
Then I used a timer to check every millisecond for new events in the
queue and send it to another thread (ThreadC).
In what thread is this?


A good question. I do not know. The code is in the same class. I think the
framework creates it's own thread for timers. But I am not sure. Does it
matter? I have a static object in the class on which I apply a lock.Then I
expect the code between the {} should be handled as a critical section from
whatever thread the method will be called. Perhaps this is a false
assumption?

I included a lock statement on a static object to ensure a critical
section. But after some time (6 hours) the timer stops.
I debugged it and found that i could even not restart the timer executing
m_EventTimer.Start().

What do I wrong? Why is the timer blocking?
I'm not very sure that you need all this, first of all why you need so
many threads doing part of the process?

In addition you don't have to use lock , simply use a Syncronized queue
(Queue.Syncronized ). then have one thread receive the message, prepare it
and place it in the queue.

Another thread will poll the queue at a regular interval and do any
process.


I will try this. Thanks.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

May 12 '06 #4
Ben

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:ca********************************@4ax.com...
"Ben" <Po************@kjkjkjkjkjkjkjkjkjkjkjkjkjkjkj.com > wrote:
I got confused by this problem for which I don't have a logical
explanation.
There is a Thread (ThreadA) which receives Events from another system
thread
(ThreadS). ThreadA then adds a time stamp to the received event and adds
it
to a event queue. This works well (therfore not shown here). The queue
fills
up.
Then I used a timer to check every millisecond for new events in the
queue
and send it to another thread (ThreadC). I included a lock statement on a
static object to ensure a critical section. But after some time (6 hours)
the timer stops.
I debugged it and found that i could even not restart the timer executing
m_EventTimer.Start().


First, System.Timers.Timer is a component for UIs, you should check out
System.Threading.Timer for working with threading.

However, you are designing your architecture around polling - you
shouldn't do that. Google "producer consumer queue", I think you should
design your architecture in a different way.
-- Barry


Ok I'll check the books. Thanks for the hint.
May 12 '06 #5

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

Similar topics

4
by: Anthony Boudouvas | last post by:
Hi to all, i have a form with 2 System.Windows.Forms.Timer objects. One fire every 5 seconds and the other every 10 seconds, the both take actions in two hashtables declared in same form. ...
2
by: Jesper Stocholm | last post by:
I have created a simple service which just copies a fil to a new file with a new name on certain intervals (the service implements a timer). I have no problems installing the service and the...
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...
0
by: kapcreations | last post by:
I have a system tray app which gets the status of a windows service, and displays the appropriate icon for the status of the service in the systray. the problem I am having is the icon disapears...
4
by: Nijazi Halimaji | last post by:
Hi everybody I have created a new timer object WithEvents tmr_check As New System.Timers.Timer On my form_Load-Event I activated the timer... Then I made a label on my form and putted on...
5
by: Chakravarti Mukesh | last post by:
Hi, I want to get an event if someone locks her/his computer so that I could do some finalizations before actually locking the system. For example how can I ensure that an user close a...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
7
by: traafat | last post by:
Hello guys, i am having a problem using Delegates, i am pretty new to C# , i am starting to fall in love with it its really good language (coming from Java and Delphi), now i am working on an...
5
by: Peted | last post by:
i have an mdi application with two child forms Childform A and childform B in a nutshell Childform B has a timer with a routine that polls a ip socket for information every 30sec.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.