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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:01:07+00:00 2026-05-24T22:01:07+00:00

I have a windows service written in C# that executes a method correctly. I

  • 0

I have a windows service written in C# that executes a method correctly. I added a timer to schedule the method execution and it doesn’t seem to fire the ElapsedEventHandler event.

System.Timers.Timer timer = new System.Timers.Timer();

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


public void SetTimer()
        {
            DateTime nextRunTime = GetNextRunTime();
            var ts = nextRunTime - DateTime.Now;
            timer.Interval = ts.TotalMilliseconds;
            timer.AutoReset = false;
            timer.Start();
        }

void timer_Elapsed(object source, ElapsedEventArgs e)
        {
           // ** never gets here **
            timer.Stop();
              // run some code
            SetTimer();
        }

I can run and hit a breakpoint at timer.Start(); so I know that’s being done, but it never falls into the timer_Elapsed method. (For testing I change ts.TotalMilliseconds to 1000) Any ideas?

  • 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-24T22:01:08+00:00Added an answer on May 24, 2026 at 10:01 pm

    From comments on the question you say you are calling it like this:

    #if (!DEBUG) 
        ServiceBase[] ServicesToRun; 
        ServicesToRun = new ServiceBase[] { new LabelLoader() }; 
        ServiceBase.Run(ServicesToRun); 
    #else 
        LabelLoader ll = new LabelLoader(); 
        ll.Start(); 
    #endif
    

    If you are in debug mode that means your main method is this:

        LabelLoader ll = new LabelLoader(); 
        ll.Start(); 
    

    This means that once it has run these two lines the program finishes running and presumably exits. It doesn’t matter what your timer is up to, the program has quit and thus your timer never fires.

    I’d advise testing your ll with a better harness. Personally I use a winform type interface and just have a start button to mimic the service start (which will then have your code in the button click). Once I think I have that code running as I want I then test it in a service environment.

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

Sidebar

Related Questions

I have a windows service written in C# that acts as a proxy for
I have a Windows Service written in C# that handles all of our external
Scenario I have a windows service written in C# that performs some processing based
We have a Windows Service written in C#. The service spawns a thread that
I have a windows service that loads multiple handlers written by different developers. The
I have written a Windows service that allows me to remotely run and stop
I have written a Windows service that spawns a separate process. This process creates
I have a server written in Java that runs as a Windows service (thanks
We have written a custom windows service (VB.NET, .NET Framework 2.0) that takes a
I have written a windows service in C# that essentially listens for commands 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.