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

System.Timers.Timer performance test ?

Hello
I'm testing the performance of the System.Timers.Timer class.
I created a small program that create 100 User objects.
Each USer object create a MyTimer object.

The constructor of the User class contains a name and a time to wait
for the timer.
Calling start on the User objet initiate the MyTimer to callback in 1
second.

When the User received the callback from MyTimer, it restart a new
timer for 1 second.

It works fine with 10 users but with 100 users and more, It seems that
few User callbacks
do not appears.

With 10 users, each second the total is : 10, 20, 30, 40, 50, ....
With 100 users the total is : 100, 200, 270, 365, .....
It's not regular !!!

Have you an idea ?
What will it be the best implemation to do that ?

Thanks

The c# program :

using System;

namespace TimersPerf
{
public delegate void MyTimerHandler (double delta);
class MyTimer
{
public event MyTimerHandler CallBack;
System.Timers.Timer t=null;
private DateTime startTime, endTime;
private int ms;

public MyTimer(int ms)
{
this.ms = ms;
}

public void Set()
{
if (t==null)
{
t = new System.Timers.Timer(ms);
t.AutoReset = false;
t.Elapsed+=new System.Timers.ElapsedEventHandler(t_Elapsed);
startTime = DateTime.Now;
t.Start();
}
}

private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
endTime = DateTime.Now;
TimeSpan delta = endTime-startTime;
t.Stop();
t.Dispose();
t = null;
CallBack(delta.TotalMilliseconds);
}

}

class User
{
private MyTimer t;
private int name;
private int ms;
private int cpt =0;

private static object obj = new object();
private static int total = 0;

public User(int name, int ms)
{
this.name = name;
this.ms = ms;
t = new MyTimer(ms);
}

public void start()
{
t.CallBack+=new MyTimerHandler(CallBack);
t.Set();
}

private void CallBack(double delta)
{
cpt++;
lock(obj)
{
total++;
Console.WriteLine("name={0}, delta={1}, cpt={2}, total={3}", name,
delta, cpt, total);
}

t.Set();
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.foo();
Console.ReadLine();
}
public void foo()
{
int count = 10;
User[] user = new User[count];
for(int i=0; i<count; i++)
{
user[i] = new User(i, 1000);
user[i].start();
}
}

}
}

Nov 28 '05 #1
1 2951
Hi,

The WinXX OS is not a real time OS , therefore you cannot be 100% sure that
an event will fire at a given time.

cheers,

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

<lo******@hotmail.fr> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello
I'm testing the performance of the System.Timers.Timer class.
I created a small program that create 100 User objects.
Each USer object create a MyTimer object.

The constructor of the User class contains a name and a time to wait
for the timer.
Calling start on the User objet initiate the MyTimer to callback in 1
second.

When the User received the callback from MyTimer, it restart a new
timer for 1 second.

It works fine with 10 users but with 100 users and more, It seems that
few User callbacks
do not appears.

With 10 users, each second the total is : 10, 20, 30, 40, 50, ....
With 100 users the total is : 100, 200, 270, 365, .....
It's not regular !!!

Have you an idea ?
What will it be the best implemation to do that ?

Thanks

The c# program :

using System;

namespace TimersPerf
{
public delegate void MyTimerHandler (double delta);
class MyTimer
{
public event MyTimerHandler CallBack;
System.Timers.Timer t=null;
private DateTime startTime, endTime;
private int ms;

public MyTimer(int ms)
{
this.ms = ms;
}

public void Set()
{
if (t==null)
{
t = new System.Timers.Timer(ms);
t.AutoReset = false;
t.Elapsed+=new System.Timers.ElapsedEventHandler(t_Elapsed);
startTime = DateTime.Now;
t.Start();
}
}

private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
endTime = DateTime.Now;
TimeSpan delta = endTime-startTime;
t.Stop();
t.Dispose();
t = null;
CallBack(delta.TotalMilliseconds);
}

}

class User
{
private MyTimer t;
private int name;
private int ms;
private int cpt =0;

private static object obj = new object();
private static int total = 0;

public User(int name, int ms)
{
this.name = name;
this.ms = ms;
t = new MyTimer(ms);
}

public void start()
{
t.CallBack+=new MyTimerHandler(CallBack);
t.Set();
}

private void CallBack(double delta)
{
cpt++;
lock(obj)
{
total++;
Console.WriteLine("name={0}, delta={1}, cpt={2}, total={3}", name,
delta, cpt, total);
}

t.Set();
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.foo();
Console.ReadLine();
}
public void foo()
{
int count = 10;
User[] user = new User[count];
for(int i=0; i<count; i++)
{
user[i] = new User(i, 1000);
user[i].start();
}
}

}
}

Nov 28 '05 #2

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

Similar topics

3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
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: brunft | last post by:
I was testing this with .NET Framework 2.0. Exceptions in the Elapsed event handler of a System.Timers.Timer are ignored, they are not handled by the runtime and not reported (when running...
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...
4
by: Ben | last post by:
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...
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...
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...
5
by: Brian | last post by:
Hello all. I have a simple application that I was using to test and understand the System.Timers.Timer and noticed that when I stop the application, on occasion, it throughs the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
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
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...
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,...

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.