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

  • Home
  • SEARCH
  • 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 4009666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:54:20+00:00 2026-05-20T08:54:20+00:00

This is a fictional example but I was wandering what happens if the InitialiseTimer

  • 0

This is a fictional example but I was wandering what happens if the InitialiseTimer function gets called twice. Does the timer elapsed function get triggered twice. Will this change if the functions are made static?

    private static void InitialiseTimer()
    {
            TheTimer = new System.Timers.Timer();
            TheTimer.Interval = 400;
            TheTimer.Elapsed += new ElapsedEventHandler(TheTimer_Elapsed);
            TheTimer.AutoReset = false;
    }   

    public void TheTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //Do stuff in here
    }

I was going to use below to prevent this

Has an event handler already been added?

Thanks,
Richard

  • 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-20T08:54:20+00:00Added an answer on May 20, 2026 at 8:54 am

    I think the following demonstrates the scenario and does indeed fire twice, also propose a simple change (commented code) to the Init method that should fix the behavior. (Not thread safe btw, additional locks would be required)

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var counter = 0;
            var ts = new ThreadStart(() =>
                {
    
                    Foo.Fired += (o, e) =>
                        {
                            counter++;
                        };
                    Foo.InitialiseTimer();
                    Foo.InitialiseTimer();
                });
            var t = new Thread(ts);
            t.Start();
    
            Thread.Sleep(30);
            Assert.AreEqual(1, counter);
        }
    }
    
    public class Foo
    {
        private static System.Timers.Timer TheTimer = null;
    
        public static event EventHandler Fired;
    
        public static void InitialiseTimer()
        {
            //if (TheTimer != null)
            //{
            //    TheTimer.Stop();
            //    TheTimer = null;
            //}
            TheTimer = new System.Timers.Timer();
            TheTimer.Interval = 10;
            TheTimer.Elapsed += new ElapsedEventHandler(TheTimer_Elapsed);
            TheTimer.AutoReset = false;
            TheTimer.Start();
        }
    
        public static void TheTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //Do stuff in here
            if (Fired != null)
            {
                Fired(null, null);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I get JTextPane to scroll? In this example, JScrollPane is employed but
The fictional example here. Suppose I designed a class named IODevice. This class already
I am using a fictional example for this. Say, I have a Widget class
I have a little example Rails app called tickets, which views and edits fictional
This somehow simple task is not so simple. I can get the number of
This should be simple, but the answer is eluding me. If I've got a
this is my code that I write it but I want to use LINQ
Hello yeah I'm asking this question a second time, sorry about that but I
Note: A have asked this question at the Superuser community first , but since
Variants of this question have been asked several times now here, but my question

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.