473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lock problems with System.Timers.T imer

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.St art().

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_El apsed(object sender,
System.Timers.E lapsedEventArgs e)
{
lock(m_EventTim erLockObject)
{
MyClass.persona lMessage tmpPersMsg;
try
{
m_EventTimer.St op();
//send all events which already occurred to the TAPI class for logging
while(m_Events. Count > 0)
{
lock(m_Events)
{
tmpPersMsg= (MyClass.person alMessage )m_Events[0];
m_Events.Remove At(0);
}
this.OnLineMess age(new PersonalMessage EventArgs(tmpPe rsMsg));
}
}
catch(Exception ex)
{
Logging.LogData base(150103, ex.Message, ex.StackTrace,
LogSeverityDB.E rror, "PM.cs"); // Event timer raised an exception.
}
finally
{
m_EventTimer.St art();
}
}
}
Alternative (right now in test):
private void OnEventTimer_El apsed(object sender,
System.Timers.E lapsedEventArgs e)
{
if(System.Threa ding.Monitor.Tr yEnter(m_EventT imerLockObject) )
{
MyClass.persona lMessage tmpPersMsg;
try
{
m_EventTimer.St op();
//send all events which already occurred to the TAPI class for logging
while(m_Events. Count > 0)
{
lock(m_Events)
{
tmpPersMsg= (MyClass.person alMessage )m_Events[0];
m_Events.Remove At(0);
}
this.OnLineMess age(new PersonalMessage EventArgs(tmpPe rsMsg));
}
}
catch(Exception ex)
{
Logging.LogData base(150103, ex.Message, ex.StackTrace,
LogSeverityDB.E rror, "PM.cs"); // Event timer raised an exception.
}
finally
{
System.Threadin g.Monitor.Exit( m_EventTimerLoc kObject);
m_EventTimer.St art();
}
}
}
May 11 '06 #1
4 5361
Hi,
"Ben" <Po************ @kjkjkjkjkjkjkj kjkjkjkjkjkjkjk j.com> wrote in message
news:uu******** ******@TK2MSFTN GP02.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.St art().

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.Syncroni zed ). 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************ @kjkjkjkjkjkjkj kjkjkjkjkjkjkjk j.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.St art().


First, System.Timers.T imer is a component for UIs, you should check out
System.Threadin g.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.mach in AT dot.state.fl.us > wrote
in message news:uu******** ******@TK2MSFTN GP02.phx.gbl...
Hi,
"Ben" <Po************ @kjkjkjkjkjkjkj kjkjkjkjkjkjkjk j.com> wrote in message
news:uu******** ******@TK2MSFTN GP02.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 = WaitForSingleOb ject(m_EventHan dle, 200);
switch(Status)
{
case WaitForObjectsR esults.WAIT_OBJ ECT_0: //a event is pending,
lineMsg = new Tapi.linemessag e(0, 0, IntPtr.Zero, IntPtr.Zero,
IntPtr.Zero, IntPtr.Zero);
if (GetTapiMessage (ref lineMsg) ==
Tapi2Lib.LineEr rReturn.LINEERR _OK)
{
//Create Event
if(m_EventThrea dIsSendingLineM essages)
{
AddEvent(lineMs g);
}
}
else
{
//No Msg (LINEERR_OPERAT IONFAILED) or error
}
break;
case WaitForObjectsR esults.WAIT_TIM EOUT: //Nothing to do
break;
default: //WaitForObjectsR esults.WAIT_FAI LED,
WaitForObjectsR esults.WAIT_ABA NDONED
//TODO : GetLastError();
break;
}

private void AddEvent(MyClas s.personalMessa ge 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.St art().

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.Syncroni zed ). 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.c om...
"Ben" <Po************ @kjkjkjkjkjkjkj kjkjkjkjkjkjkjk j.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.St art().


First, System.Timers.T imer is a component for UIs, you should check out
System.Threadin g.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
4020
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. When timers fire, main form is somewhat blocking until timers finish their job, (socket operations). (Imagine to move the form by it's caption bar and it somewhat freeze when timers fire...)
2
4707
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 log-entries I have created on stop() and start() in the eventlog works fine as well. However - the actual task is never executed. I catch the execution of the tick and in the finally-block I write to the eventlog as well. But only one entry is creates...
9
7861
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 might need to carry out some back-end processing without pausing the individual users' Session while that process runs. E.g. I might provide the ability for a user to upload a text file of job
0
1822
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 under certain conditions, but the exe is still listed in the processes leading me to believe the app is still running. It fires up perfectly on boot, however if the user logs off, and then logs back in (not reboot), or if the system goes to...
4
3067
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 the Elapsed-Event of my timer object following code to change the Label.Text Label1.Text = "Ola"
5
2505
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 particular application before locking her/his PC. Thanks Mukesh
4
11110
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 the default of True. The Elapsed event handler updates the dialog box with how long before it will close, acting as a timer itself. The dialog has a time to close property which is checked every time the Elapsed event fires. The problem I have...
7
1478
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 application where i have a label on a form and then i have a separate class in separate CS file (ie., Form1.cs and myClass.cs)
5
3020
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
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.