SCM

[#1010997] Consider using System.Threading.Timer instead of System.Timers.Timer

View Trackers | Bugs | Download .csv | Monitor

Date:
2011-02-22 13:05
Priority:
3
State:
Open
Submitted by:
Alex A Ermoshenko (erax)
Assigned to:
Nobody (None)
Npgsql Version:
None
Category:
Group:
Resolution:
None
Summary:
Consider using System.Threading.Timer instead of System.Timers.Timer

Detailed description
In real world, with busy database server and ping from machine with Npgsql client to database server greater than 100. System.Threading.Timer creates up to 100 threads(see screenshot) while wait in function NpgsqlConnectorPool.TimerElapsedHandler


private void TimerElapsedHandler(object sender, ElapsedEventArgs e)
{
NpgsqlConnector Connector;
lock (this) < ------- Waits here. System.Timers.Timer creates recurring threads
{
foreach (ConnectorQueue Queue in PooledConnectors.Values)
.............

Followup

Message
Date: 2011-03-15 01:25
Sender: Francisco Figueiredo jr.

committed to cvs.

Please, give it a try and let me know if it works for you.
Date: 2011-02-23 17:14
Sender: Francisco Figueiredo jr.


Got it!

Much clever approach than I was thinking about...

Thanks!
Date: 2011-02-23 12:43
Sender: Alex A Ermoshenko

You're right!

I fix this:
0: Use System.Timers.Timer
1: Timer.AutoReset = false;// set to false
2: rewrite TimerElapsedHandler method
private void TimerElapsedHandler(object sender,
ElapsedEventArgs e)
{
try
{
//curent method contents
...
}
finally
{
Timer.Start();
}
}

Alex


Date: 2011-02-22 14:38
Sender: Alex A Ermoshenko

You can manually create whe Background worker thread for
this, and in infinite loop use Thread.Sleep(1000) to create
one second intervals :-)

PS Remember, thread must be background!
Date: 2011-02-22 14:02
Sender: Francisco Figueiredo jr.


This may be happening because the handler is taking too long to return. System.Threading.Timer also uses thread pool so, I think even if we change Npgsql to use it, the threads would stack up again. Maybe I'm missing something?

I think we have to improve this implementation to call the event handler just once at a time. I'm reading the documentation in order to implement something about that. This way, by having the it called only once would prevent this locking from happening.

Attached Files:

Attachments:
npgsql_timer.PNG

Changes:

Field Old Value Date By
File Added590: npgsql_timer.PNG2011-02-22 13:05erax
Powered By FusionForge