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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:56:20+00:00 2026-05-25T00:56:20+00:00

I created a windows service with a timer in it, where I need to

  • 0

I created a windows service with a timer in it, where I need to set set the interval after each Elapsed timer event. For example, I’d like it to fire on the hour every hour.

In Program.cs:

namespace LabelLoaderService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        static void Main()
        {
#if (!DEBUG)
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new LabelLoader() 
            };
            ServiceBase.Run(ServicesToRun);
#else
            LabelLoader ll = new LabelLoader();
            ll.Start();
#endif

        }
    }
}

In LabelLoader.cs:

namespace LabelLoaderService
{
    public partial class LabelLoader : ServiceBase
    { 
       System.Timers.Timer timer = new System.Timers.Timer();

    public LabelLoader()
    {
        InitializeComponent();
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    }

    protected override void OnStart(string[] args)
    {
        SetTimer();
    }


    public void Start()
    {
        // Debug Startup            
        SetTimer();
    }


    public void SetTimer()
    {
        DateTime nextRunTime = GetNextRunTime();
        var ts = nextRunTime - DateTime.Now;
        timer.Interval = ts.TotalMilliseconds;
        timer.AutoReset = true;  // tried both true & false
        timer.Enabled = true;
        GC.KeepAlive(timer);  // test - no effect with/without this line
    }

    void timer_Elapsed(object source, ElapsedEventArgs e)
    {
        timer.Enabled = false;
        // do some work here
        SetTimer();
    }

If I installutil this onto my local machine, it correctly determines the next run time and executes. But it doesnt run anytime after that. If I restart the service it runs the next scheduled time and then nothing again. Is there an issue calling SetTimer() at the end of my processing to reset the Interval and set timer.Start()?

  • 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-25T00:56:21+00:00Added an answer on May 25, 2026 at 12:56 am

    use System.Threading.Timer instead – in my experience it is more suited for server-like use…

    EDIT – as per comment some code/hints:

    the following is a very basic way to avoid reentry (should work ok in this specific case) – better would be some lock/Mutex or similar
    make nextRunTime an instance field
    create/start your time with for example

    // first the TimerCallback, second the parameter (see AnyParam), then the time to start the first run, then the interval for the rest of the runs
    timer = new System.Threading.Timer(new TimerCallback(this.MyTimerHandler), null, 60000, 30000);
    

    create your timer handler similar to

    void MyTimerHandler (object AnyParam)
    {
    if ( nextRunTime > DateTime.Now) 
         return;
    
    nextRunTime = DateTime.MaxValue; 
    // Do your stuff here
    // when finished do 
    nextRunTime = GetNextRunTime();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created windows service. I need to run that automatically after 2 minutes.
I need to write a windows service which executes an application after a certain
I have created a Windows Service in VB.net for building xML from a database.
I've created a Windows Service that uses WCF for communications to it. The service
Scenario I've created a windows service, but whenever I start it, it stops immediately.
I'm using VB9 (VS2008). I've created a Windows Service that indexes some folders regularly.
I have created a C# Windows service but it fails to start. I get
I have created a simple windows service that periodically checks a remote database via
We have created a WCF service hosted in a windows service that handles Authentication
I have created an installation package using Wix which installs a Windows service on

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.