Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 436893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:31:25+00:00 2026-05-12T20:31:25+00:00

I’ve been reading Illustrated C# 2008 by Daniel Solis (an excellent book, btw) and

  • 0

I’ve been reading Illustrated C# 2008 by Daniel Solis (an excellent book, btw) and decided to spend a little more time on Events to further my understanding of the topic. I’m trying to understand why I’m seeing different results every time I run the program and what I might be able to learn from this.

(Source Code Below) In the sample code in the book, there’s a MyTimerClass which has an event which is subscribed to System.Timers.Timer. There are two other classes, ClassA and ClassB, both which have Event Handlers (one is static while the other is not) that write to the console. In the main function of the program, the event handlers of those classes are tied up to an event in an instance of the MyTimerClass. Another function is added to the event member via a lambda expression.

After working with the author’s code, I decided to add another class, ClassC. Rather than add the event handler within the Main function of the program, I instead decided to create a separate MyTimerClass object within ClassC‘s constructor that then subscribed to MyTimerClass‘s event.

When I ran my code for 4.25 seconds on 3 separate occasions, my results were always in a different order. The Event from the Main function of the order always invoked ClassA, ClassB, then Lambda in the same order. However, the other event for ClassC always seems to be invoked in a completely random order. I also notice that the first group of method calls seems to have slightly different times, while the subsequent groups all have the same times. Why is that?

(1) Event 1 - ClassA - 51:259
    Event 2 - ClassC - 51:259
(2) Event 1 - ClassB - 51:261
(3) Event 1 - Lambda - 51:262
(1) Event 1 - ClassA - 52:271
(2) Event 1 - ClassB - 52:271
(3) Event 1 - Lambda - 52:271
    Event 2 - ClassC - 52:271
(1) Event 1 - ClassA - 53:285
(2) Event 1 - ClassB - 53:285
(3) Event 1 - Lambda - 53:285
    Event 2 - ClassC - 53:285
(1) Event 1 - ClassA - 54:299
(2) Event 1 - ClassB - 54:299
(3) Event 1 - Lambda - 54:299
    Event 2 - ClassC - 54:299

(1) Event 1 - ClassA - 17:30
    Event 2 - ClassC - 17:30
(2) Event 1 - ClassB - 17:32
(3) Event 1 - Lambda - 17:33
(1) Event 1 - ClassA - 18:42
(2) Event 1 - ClassB - 18:42
(3) Event 1 - Lambda - 18:42
    Event 2 - ClassC - 18:42
(1) Event 1 - ClassA - 19:56
(2) Event 1 - ClassB - 19:56
(3) Event 1 - Lambda - 19:56
    Event 2 - ClassC - 19:56
    Event 2 - ClassC - 20:70
(1) Event 1 - ClassA - 20:70
(2) Event 1 - ClassB - 20:70
(3) Event 1 - Lambda - 20:70

(1) Event 1 - ClassA - 45:220
    Event 2 - ClassC - 45:221
(2) Event 1 - ClassB - 45:223
(3) Event 1 - Lambda - 45:223
(1) Event 1 - ClassA - 46:232
(2) Event 1 - ClassB - 46:232
(3) Event 1 - Lambda - 46:232
    Event 2 - ClassC - 46:232
    Event 2 - ClassC - 47:246
(1) Event 1 - ClassA - 47:246
(2) Event 1 - ClassB - 47:246
(3) Event 1 - Lambda - 47:246
(1) Event 1 - ClassA - 48:260
(2) Event 1 - ClassB - 48:260
(3) Event 1 - Lambda - 48:260
    Event 2 - ClassC - 48:260

Here’s the source code for my Console Application:

class Program
{
    static void Main(string[] args)
    {
        MyTimerClass mc = new MyTimerClass();
        ClassA ca = new ClassA();
        ClassC cc = new ClassC();

        mc.MyElapsed += ca.TimerHandlerA;
        mc.MyElapsed += ClassB.TimerHandlerB;
        mc.MyElapsed += (obj, e) =>
            {
                Console.WriteLine("(3) Event 1 - Lambda - {0}:{1}",
                    System.DateTime.Now.Second,
                    System.DateTime.Now.Millisecond);
            };

        Thread.Sleep(4250);
    }
}

class ClassA
{
    public void TimerHandlerA(Object obj, EventArgs e)
    {
        Console.WriteLine("(1) Event 1 - ClassA - {0}:{1}",
            System.DateTime.Now.Second,
            System.DateTime.Now.Millisecond);
    }
}

class ClassB
{
    public static void TimerHandlerB(Object obj, EventArgs e)
    {
        Console.WriteLine("(2) Event 1 - ClassB - {0}:{1}",
            System.DateTime.Now.Second,
            System.DateTime.Now.Millisecond);
    }
}

class ClassC
{
    public void TimerHandlerC(Object obj, EventArgs e)
    {
        Console.WriteLine("    Event 2 - ClassC - {0}:{1}",
            System.DateTime.Now.Second,
            System.DateTime.Now.Millisecond);
    }

    public ClassC()
    {
        // This will create a separate MyTimerClass and
        // attach ClassC's event handler to mc's event.
        MyTimerClass mc = new MyTimerClass();
        mc.MyElapsed += TimerHandlerC;
    }
}

public class MyTimerClass
{
    public event EventHandler MyElapsed;

    private void OnOneSecond(Object obj, EventArgs e)
    {
        if (MyElapsed != null)
            MyElapsed(obj, e);
    }

    private System.Timers.Timer MyPrivateTimer;

    public MyTimerClass()
    {
        MyPrivateTimer = new System.Timers.Timer();

        // This will attach the OnOneSecond Event Handler
        // to the system timer which will then raise
        // MyElapsed.
        MyPrivateTimer.Elapsed += OnOneSecond;

        // This sets the interval at 1 second.
        MyPrivateTimer.Interval = 1000;

        // This turns the timer on when the the class
        // is instantiated.
        MyPrivateTimer.Enabled = true;
    }
}

Three questions:

  • Why is it that the results are different every time and what is causing that to happen?
  • Why are the times in the first block of results slightly off while the subsequent blocks have the same times?
  • What should I learn from this example?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:31:25+00:00Added an answer on May 12, 2026 at 8:31 pm

    The short answer is, “that’s just the way Windows Timers work.” You’re not doing anything wrong here, and the results you see are normal behavior.

    The native timers provided by Microsoft in Windows are not guaranteed to be precise in their ticking. If you set a timer to tick every 1000 milliseconds, it’s guaranteed that it will be at least 1000 milliseconds, but not exactly 1000 milliseconds. It will usually be very close, and sometimes exact, but if you need very high precision timing you’ll need to look into other mechanisms. At the Win32 C/C++ API level, the appropriate mechanisms are the QueryPerformanceFrequency/QueryPerformanceCounter methods. In .NET, you’d use the StopWatch class to get this functionality.

    As for the order, if you attach multiple handlers to one timer in .NET, I’m pretty sure that there is also no guarantee that they will be executed in any given order. I’m not sure exactly what algorithm Microsoft is using under the hood to fire them, but your results clearly show that it’s not an algorithm that guarantees them to be in order. If you need the code to be in order, then register one handler for the timer and call everything from that handler, in the proper order.

    As noted in the comments, Windows isn’t really designed to be a high performance real-time OS. However, that’s not really necessary for 99.99% of desktop applications (hence Microsoft’s design decision; it is, after all, primarily a desktop OS).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.