Click here to Skip to main content
16,003,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a windows service which should run for every one hour. But the issue is Ex: it starts at 6.00 A.m and ends at 6.03 A.M. Then again after one hour its starting running at 7.03 A.M. But I want it to run again at 7.00 A.M.
Anyone's help will be appreciated.
The code What I have written is below

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ashworth.WindowService.GradeDataFeed
{
    public partial class GradeDataFeedService : ServiceBase
    {
            private readonly System.Timers.Timer _timer;
            GradeDataFeedManager gradeDataFeedManager;
            //Thread managerThread = null;

            public GradeDataFeedService()
            {
                InitializeComponent();

                _timer = new System.Timers.Timer();
                _timer.Elapsed += timer_Elapsed;
            }

            protected override void OnStart(string[] args)
            {
                _timer.Interval = 60 * 1000;
                _timer.Enabled = true;
                _timer.Start();
            }

            private void timer_Elapsed(object sender, EventArgs e)
            {
                // Only create / start the manager if has been done so already
                // It should only all in once, since the manager object will not
                // be null and since the timer will be disabled after first run
                if (gradeDataFeedManager == null)
                {
                gradeDataFeedManager = new GradeDataFeedManager();

                // Create thread for manager
                //managerThread = new Thread(shipmentTrackingNumberProcessorManager.Start);

                // Start the thread
                gradeDataFeedManager.Start();

                    // Stop timer, it only needs to run once to kick off the manager
                    _timer.Enabled = false;
                }
            }

            protected override void OnStop()
            {
                if (gradeDataFeedManager != null)
                {
                gradeDataFeedManager.Stop();
                }
            }
        }
    }
Posted
Updated 3-Oct-17 6:22am
Comments
F-ES Sitecore 3-Oct-17 12:30pm    
You could re-initialise the timer after the work is done, so calculate the number of seconds until the next hour and set that as the interval and kick the timer off again. Alternatively have it wake up every second and see if it's the next hour yet.

1 solution

First off, you don't need a service. You need a simple console app that runs from a scheduled task

Otherwise you have to check the current time every, say 1 second, to see if the current time is on the hour
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900