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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:20:00+00:00 2026-06-06T00:20:00+00:00

i have a windows service set to run at certain times, it runs first

  • 0

i have a windows service set to run at certain times, it runs first time but i cant seem to work out how to tell it to run the next day when it gets to end of times..

this is what i am trying…

protected override void OnStart(string[] args)
    {
        log.Info("Info - Service Start");
        string timeToRunStr = "17:11";
        var timeStrArray = timeToRunStr.Split(';');
        foreach (var strTime in timeStrArray)
        {
            timeToRun.Add(TimeSpan.Parse(strTime));
        }
        ResetTimer();
    }

    void ResetTimer()
    {
        log.Info("Info - Reset Timer");
        try
        {
            TimeSpan currentTime = DateTime.Now.TimeOfDay;
            TimeSpan nextRunTime = timeToRun[0];
            foreach (TimeSpan runTime in timeToRun)
            {
                if (currentTime < runTime)
                {
                    nextRunTime = runTime;
                    // log.Info("Info - in loop");
                    break;
                }
                else {
                    TimeSpan test = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 16, 51, 0).Subtract(DateTime.Now);
                    nextRunTime = test;
                }
            }
            _timer = new Timer((nextRunTime - currentTime).TotalSeconds * 1000);
            log.Info("Info - Timer : " + (nextRunTime - currentTime).TotalSeconds * 1000);
            log.Info("Info - nextRuntime : " + nextRunTime);
            log.Info("Info - currentTime : " + currentTime);
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            _timer.Start();
        }
        catch (Exception ex)
        {
            log.Error("This is my timer error - ", ex);
        }
    }
  • 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-06-06T00:20:01+00:00Added an answer on June 6, 2026 at 12:20 am

    check out these alternatives: :
    DateTime of next 3am occurrence

    Control activities with a timer, see model:

    class Program
    {
        static void my_task(Object obj)
        {
            Console.WriteLine("task being performed.");
        }
    
        static TimerCallback timerDelegate;
        static Timer timer;
    
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;
            DateTime today = now.Date.AddHours(12);
            DateTime next = now <= today ? today : today.AddDays(1);
    
            timerDelegate = new TimerCallback(my_task);
    
            //                                     hence the first         after the next       
            timer = new Timer(timerDelegate, null, next - DateTime.Now, TimeSpan.FromHours(24));
        }
    }
    
    • 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 that has it's name set by an app.config. I
I have a windows service which runs continuously and creates some threads to do
I have a windows service that does some intensive work every one minute (actually
I have a windows service, written in c# and I need to run a
I have a Windows service that runs as a logged-in user (local admin). During
I have a windows service set to copy files from a local directory to
I have created windows service. I need to run that automatically after 2 minutes.
I have just written a program to run as a Windows Service Application, GasMeterMonitoring;
Say I have a windows service that runs a method to generate reports. For
I have created a Windows Service that will be calling out to some COM

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.