B
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_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
{
System.Threading.Monitor.Exit(m_EventTimerLockObject);
m_EventTimer.Start();
}
}
}
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_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
{
System.Threading.Monitor.Exit(m_EventTimerLockObject);
m_EventTimer.Start();
}
}
}