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 931159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:27:22+00:00 2026-05-15T20:27:22+00:00

i have been using System.Timer to run a windows service but have come across

  • 0

i have been using System.Timer to run a windows service but have come across the problem where the timer randomly doesnt fire. I checked it yesterday and it hadnt fired for over 2 hours when its meant to fire every 10 mins. I read this up on Google and apparently its a known problem, the answer being to change over to Threading.Timer. I havent used this before so was looking for some insight. My current code is as follows:

using System;
using System.Timers;
using System.ServiceProcess;

namespace Code
{
public partial class Service : ServiceBase
{
    Timer timer = new Timer();

    public Service()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = 10000;
        timer.Enabled = true;
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        timer.Enabled = false;
        // Run system code here
        timer.Interval = 600000;
        timer.Enabled = true;
    }
}

}

Basically, this normally works fine. The system starts the timer and fires after 10 seconds. It stops the timer, does the job, resets the timer for 10 minutes and enables it. For the most part this always works, but as mentioned randomly decides to stop working, probably due to system resources etc.

If anyone can help me convert this into a Threading.Timer it would be appreciated.

Thanks,
Chris

  • 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-15T20:27:23+00:00Added an answer on May 15, 2026 at 8:27 pm

    Here’s my best guess – not got time to test it, sorry 🙁

    using System;
    using System.Threading;
    using System.ServiceProcess;
    
    namespace Code
    {
        public partial class Service : ServiceBase
        {
            Timer timer;
            AutoResetEvent autoEvent;
            bool stopped = true;
    
            public Service()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                stopped = false;
                TimerCallback tcb = new TimerCallback(OnElapsedTime);
                timer = new Timer(tcb, null, 10000, 600000);
            }
    
            protected override void OnStop()
            {
                stopped = true;
                timer.Dispose();
            }
    
            private void OnElapsedTime(Object stateInfo)
            {
                if (stopped)
                    return;
    
                // Run system code here
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using the CodeIgniter system for a while now - but it
I have been using this so far system 'strings binary-file.dmp | grep search_string' Is
Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
I have been using Visual Studio 2005 under Windows XP Pro 64-bit for C
I have just written a program to run as a Windows Service Application, GasMeterMonitoring;
Was using Java Timer, Then switched to ScheduledExecutorService, but my problem is not fixed.
I have been using java for long time but this question came to my
I have been using TortoiseSVN for some time and I really like it. I
I have been using Stanford POS Tagger to tag parts of speech in a
I have been using play 1.2.5rc4 for development of one app and I have

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.