Windows Service to run a function at specified time - c#

I wanted to start a Windows service to run a function everyday at specific time.
What method i should consider to implement this? Timer or using threads?

(1) On first start, Set _timer.Interval to the amount of milliseconds between the service start and schedule time. This sample set schedule time to 7:00 a.m. as _scheduleTime = DateTime.Today.AddDays(1).AddHours(7);
(2) On Timer_Elapsed, reset _timer.Interval to 24 hours (in milliseconds) if current interval is not 24 hours.
System.Timers.Timer _timer;
DateTime _scheduleTime;
public WinService()
{
InitializeComponent();
_timer = new System.Timers.Timer();
_scheduleTime = DateTime.Today.AddDays(1).AddHours(7); // Schedule to run once a day at 7:00 a.m.
}
protected override void OnStart(string[] args)
{
// For first time, set amount of seconds between current time and schedule time
_timer.Enabled = true;
_timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
}
protected void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// 1. Process Schedule Task
// ----------------------------------
// Add code to Process your task here
// ----------------------------------
// 2. If tick for the first time, reset next run to every 24 hours
if (_timer.Interval != 24 * 60 * 60 * 1000)
{
_timer.Interval = 24 * 60 * 60 * 1000;
}
}
Edit:
Sometimes people want to schedule the service to start at day 0, not tomorrow so they change DateTime.Today.AddDays(0).If they do that and set a time in the past it causes an error setting the Interval with a negative number.
//Test if its a time in the past and protect setting _timer.Interval with a negative number which causes an error.
double tillNextInterval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
if (tillNextInterval < 0) tillNextInterval += new TimeSpan(24, 0, 0).TotalSeconds * 1000;
_timer.Interval = tillNextInterval;

Are you sure, you need a service, that runs only one time per day?
Maybe Windows Task Schedule will be better solution?

Good answer (I used your code), but one problem with this line:
_timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
If DateTime.now is later than scheduleTime, you will go negative and this will generate an exception when assigning to timer.Interval.
I used:
if (DateTime.now > scheduleTime)
scheduleTime = scheduleTime.AddHours(24);
Then do the subtraction.

Use Windows built in Task Scheduler (http://windows.microsoft.com/en-us/windows7/schedule-a-task) or Quartz.net.
Unless ... you have a service that's doing lots of other processing and needs to be running all the time in which case a Timer might be appropriate.

private static double scheduledHour = 10;
private static DateTime scheduledTime;
public WinService()
{
scheduledTime = DateTime.Today.AddHours(scheduledHour);//setting 10 am of today as scheduled time- service start date
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime now = DateTime.Now;
if (scheduledTime < DateTime.Now)
{
TimeSpan span = now - DateTime.Now;
scheduledTime = scheduledTime.AddMilliseconds(span.Milliseconds).AddDays(1);// this will set scheduled time to 10 am of next day while correcting the milliseconds
//do the scheduled task here
}
}

You can do it with a thread and an event; a timer is not necessary.
using System;
using System.ServiceProcess;
using System.Threading;
partial class Service : ServiceBase
{
Thread Thread;
readonly AutoResetEvent StopEvent;
public Service()
{
InitializeComponent();
StopEvent = new AutoResetEvent(initialState: false);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
StopEvent.Dispose();
components?.Dispose();
}
base.Dispose(disposing);
}
protected override void OnStart(string[] args)
{
Thread = new Thread(ThreadStart);
Thread.Start(TimeSpan.Parse(args[0]));
}
protected override void OnStop()
{
if (!StopEvent.Set())
Environment.FailFast("failed setting stop event");
Thread.Join();
}
void ThreadStart(object parameter)
{
while (!StopEvent.WaitOne(Timeout(timeOfDay: (TimeSpan)parameter)))
{
// do work here...
}
}
static TimeSpan Timeout(TimeSpan timeOfDay)
{
var timeout = timeOfDay - DateTime.Now.TimeOfDay;
if (timeout < TimeSpan.Zero)
timeout += TimeSpan.FromDays(1);
return timeout;
}
}

if it is one in a day, why you don't use task scheduler?
windows service is useful when you want run task many time in minute. so if you want to run a program in a specific time, its better to use task scheduler and set event of task scheduler on a specific time in day.
I do many thing with task scheduler and its perfect.
you can set rout of program in task scheduler and set interval time to run it.
if you want to run a program every 5 min in a day still you can use task scheduler and its better way.

Related

c# timer synchronize with system clock

I have a timer that calls the load_to_DB method every 30 minutes. But how to make it call the method every 30 minutes in synchronize with the system time? For example: at 12:35, then at 13:05, 13:35, 14:05 etc.
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1800000;
timer.Elapsed += load_to_DB;
timer.Start();
Console.ReadLine();
}
// This method is called every 30 mins
static void load_to_DB(object sender, System.Timers.ElapsedEventArgs e)
{
//method
}
try this if you need to manually set your start date and time :
private static DateTime _NextCallTime;
private static int MinSteps = 30;
static void Main(string[] args)
{
// you can manually set start Date and Time
_NextCallTime = DateTime.Parse ("2020/5/12 12:35");
System.Timers.Timer timer = new System.Timers.Timer()
{Interval= 31000, Enabled = true};
timer.Elapsed += timer_Handler;
Console.ReadLine();
}
// this timer checks every 31 seconds
static void timer_Handler(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now.Date == _NextCallTime.Date
&& DateTime.Now.Hour == _NextCallTime.Hour
&& DateTime.Now.Minute == _NextCallTime.Minute)
{
_NextCallTime = _NextCallTime.AddMinutes(MinSteps);
load_to_DB();
}
}
// This method is called every 30 mins at spesific periods (12:35 - 1:5 ...)
static void load_to_DB()
{
//method
}
also if you need to just set start time only, not the date:
// you can manually set start Time
_NextCallTime = DateTime.Parse($"{DateTime.Now.ToString("yyyy/MM/dd",System.Globalization.CultureInfo.InvariantCulture)} 12:35");
Obviously the timer you are using is an interval timer and will repeat every interval from whenever start is called.
One way to implement your schedule is to work out the different between now and the next time you want to schedule (e.g. at 12:02, you want to set the interval to 3 minutes, but at 12:05 you want to set the interval to be 30 minutes etc).

Windows Services not found after 3 hours about

I have a windows service, this service every x second or minute, should perform a task. I have write this service, install it on my SO (Windows 7), so it work for three hours about, then the state of service is "Running" but it has ceased to perform the task. I think, the timer is in crash.
This is a code:
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("AutomaticallyRunScript"))
{
System.Diagnostics.EventLog.CreateEventSource(
"AutomaticRunScript", "LoggerAutomaticRunScript");
}
log.Info("preparazione file di config in corso...");
//set parameter from config File
setParameterFromAppConfig();
// get today's date at hours:minutes
DateTime tenAM = DateTime.Today.AddHours(hours);
tenAM = tenAM.AddMinutes(minutes);
int timeToFirstExecution = 0;
// if hours:minutes has passed, get tomorrow at hours:minutes
if (DateTime.Now > tenAM)
tenAM = tenAM.AddDays(1);//add 1 day of data
// calculate the number of milliseconds in hoursFrequency, minutesFrequency, secondsFrequency time.
int timeBetweenCalls = (int)new TimeSpan(hoursFrequency,
minutesFrequency, secondsFrequency).TotalMilliseconds;
// if hours:minutes has passed, get tomorrow at hours:minutes
//only if the frequency of the run script is every day
if (DateTime.Now > tenAM && hoursFrequency == 24)
{
tenAM = tenAM.AddDays(1);//add 1 day of data
// calculate milliseconds until the next hours:minutes .
timeToFirstExecution = (int)tenAM.Subtract(DateTime.Now).TotalMilliseconds;
}
else
{
timeToFirstExecution = (int)DateTime.Now.AddMilliseconds(timeBetweenCalls).Subtract(DateTime.Now).TotalMilliseconds;
}
// set the method to execute when the timer executes.
TimerCallback methodToExecute = ProcessFile;
// start the timer. The timer will execute "ProcessFile" when the number of seconds between now and
// the next hours:minutes elapse. After that, it will execute every 24 hours.
System.Threading.Timer timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls);
// Block the main thread forever. The timer will continue to execute.
//Thread.Sleep(Timeout.Infinite);
}
protected override void OnStart(string[] args)
{
//
}
protected override void OnStop(string[] args)
{
//
}
public void ProcessFile(object obj)
{
try
{
// do your processing here.
String percorsoFiles = ConfigurationManager.AppSettings["NameFileBat"];
string[] percorsoFile = percorsoFiles.Split(';');
foreach (string filebatch in percorsoFile)
{
//log.Info(": EXECUTE BATCH FILE " + filebatch + " NOW");
EventLog.WriteEntry("EXECUTE BATCH FILE " + filebatch + " NOW", EventLogEntryType.Information);
System.Diagnostics.Process.Start(filebatch);
//System.Threading.Thread.Sleep(elapsedTimeBetween2BatchFile);
}
EventLog.WriteEntry("***FINISHED***", EventLogEntryType.Information);
//log.Info("***FINISHED***");
}
catch (Exception e)
{
EventLog.WriteEntry("error, see log file ", EventLogEntryType.Error);
log.Error("errore: " + e);
}
}
I have see the log file and there isn't an error log
Ok, where do I start...
First of all, put as little code as possible into the constructor. All code required to initialize the service should be in OnStart, all code required to stop the service (i.e. code to stop the timer) should be in OnStop. That's the first thing.
Then it is good practice to stop a timer while the timer event is executing and to restart it in a finally block at the end of the timer event.
Third I recommend not to use System.Threading.Timer but System.Timers.Timer which is easier to handle and allows for "one shot" timers, which force you to restart the timer in code. Less things can go wrong here.
Also, do never ever create timers as local variables! They may get garbage collected any time after the method is out of focus.
And why is everybody using ConfigurationManager.AppSettings when it is so much easier and less error-prone to write Properties.Settings.Default.NameFileBat?
Short example of how to use System.Timers.Timer in service:
private System.Timers.Timer myTimer;
protected override void OnStart(string[] args)
{
myTimer = new Timer(5000); // Every 5 seconds
myTimer.AutoReset = false; // Only 1 event!!
myTimer.Elapsed += TimerElapsed; // Event handler
myTimer.Start(); // Start the timer
}
protected override void OnStop()
{
myTimer.Stop();
myTimer = null;
}
privated void TimerElapsed(object source, ElapsedEventArgs e)
{
try
{
// Do stuff
}
finally
{
// Restart if timer variable is not null
if (myTimer != null)
myTimer.Start();
}
}

Background Worker Check For When It's Midnight?

I want to create a background worker for a WinForm that triggers code whenever midnight rolls by.
I have an idea of how to do it, but I'm pretty sure it's not the best way to do it.
while(1==1)
{
//if Datetime.Now == midnight, execute code
//sleep(1second)
}
Use a System.Timers.Timer and at application start up just calculate the difference between DateTime.Now and DateTime.Today.AddDays(0). Then set the interval for that amount.
I actually did something just like this recently:
public static class DayChangedNotifier
{
private static Timer timer;
static DayChangedNotifier()
{
timer = new Timer(GetSleepTime());
timer.Elapsed += (o, e) =>
{
OnDayChanged(DateTime.Now.DayOfWeek);
timer.Interval = this.GetSleepTime();
};
timer.Start();
SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
}
private static void SystemEvents_TimeChanged(object sender, EventArgs e)
{
timer.Interval = GetSleepTime();
}
private static double GetSleepTime()
{
var midnightTonight = DateTime.Today.AddDays(1);
var differenceInMilliseconds = (midnightTonight - DateTime.Now).TotalMilliseconds;
return differenceInMilliseconds;
}
private static void OnDayChanged(DayOfWeek day)
{
var handler = DayChanged;
if (handler != null)
{
handler(null, new DayChangedEventArgs(day));
}
}
public static event EventHandler<DayChangedEventArgs> DayChanged;
}
AND:
public class DayChangedEventArgs : EventArgs
{
public DayChangedEventArgs(DayOfWeek day)
{
this.DayOfWeek = day;
}
public DayOfWeek DayOfWeek { get; private set; }
}
Useage: DayChangedNotified.DayChanged += ....
Instead you could user a Timer and set the timer tick interval to be the time between Now() and midnight.
I have no idea why polling solutions were voted up when Microsoft solved this type of problem years ago by adding a windows service to handle timing. Just create a scheduled task to run the exe. No extra overhead.
you can use Quartz to schedule that. Maybe is like a cannon to kill a mosquito in this scenario, but that's is the only scheduling job framework i know and works excellent.
Don't use polling. Instead, set up a timer task, set it to fire at midnight, and add an event to process.
TimeSpan timeBetween = DateTime.Today.AddDays(1) - DateTime.Now;
System.Timers.Timer t = new System.Timers.Timer();
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Interval = 1000 * timeBetween.Seconds;
t.Start();
I'm a little confuse about why you need a WinForm, will it be running at midnight? If all you need is some sort process to run, use the windows scheduler to run it at midnight. (On XP, but I believe Win server should be similar)Control Panel -> Scheduled Tasks -> Add Scheduled Task -> Fill out the wizard. Save you a lot of coding.

.NET, event every minute (on the minute). Is a timer the best option?

I want to do stuff every minute on the minute (by the clock) in a windows forms app using c#. I'm just wondering whats the best way to go about it ?
I could use a timer and set its interval to 60000, but to get it to run on the minute, I would have to enable it on the minute precisely, not really viable.
I could use a timer and set its interval to 1000. Then within its tick event, I could check the clocks current minute against a variable that I set, if the minute has changed then run my code. This worries me because I am making my computer do a check every 1 second in order to carry out work every 1 minutes. Surely this is ugly ?
I'm using windows forms and .Net 2.0 so do not want to use the DispatchTimer that comes with .Net 3.5
This must be a fairly common problem. Have any of you a better way to do this?
Building on the answer from aquinas which can drift and which doesn't tick exactly on the minute just within one second of the minute:
static System.Timers.Timer t;
static void Main(string[] args)
{
t = new System.Timers.Timer();
t.AutoReset = false;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Interval = GetInterval();
t.Start();
Console.ReadLine();
}
static double GetInterval()
{
DateTime now = DateTime.Now;
return ((60 - now.Second) * 1000 - now.Millisecond);
}
static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString("o"));
t.Interval = GetInterval();
t.Start();
}
On my box this code ticks consistently within .02s of each minute:
2010-01-15T16:42:00.0040001-05:00
2010-01-15T16:43:00.0014318-05:00
2010-01-15T16:44:00.0128643-05:00
2010-01-15T16:45:00.0132961-05:00
How about:
int startin = 60 - DateTime.Now.Second;
var t = new System.Threading.Timer(o => Console.WriteLine("Hello"),
null, startin * 1000, 60000);
Creating a Timer control that fires every 1 second (and usually does nothing but a simple check) will add negligible overhead to your application.
Simply compare the value of Environment.TickCount or DateTime.Now to the last stored time (the previous 'minute tick'), and you should have a reasonably precise solution. The resolution of these two time values is about 15ms, which should be sufficient for your purposes.
Do note however that the interval of the Timer control is not guaranteed to be that precise or even anywhere now, since it runs on the Windows message loop, which is tied in with the responsiveness of the UI. Never rely on it for even moderately precise timing - though it is good enough for firing repeating events where you can check the time using a more sensitive method such as one of the two given above.
You can nail this with reactive extensions which will take care of lots of timer related problems for you (clock changes, app hibernation etc). Use Nuget package Rx-Main and code like this:
Action work = () => Console.WriteLine(DateTime.Now.ToLongTimeString());
Scheduler.Default.Schedule(
// start in so many seconds
TimeSpan.FromSeconds(60 - DateTime.Now.Second),
// then run every minute
() => Scheduler.Default.SchedulePeriodic(TimeSpan.FromMinutes(1), work));
Console.WriteLine("Press return.");
Console.ReadLine();
Read here (search for "Introducing ISchedulerPeriodic") to see all the issues this is taking care of: http://blogs.msdn.com/b/rxteam/archive/2012/06/20/reactive-extensions-v2-0-release-candidate-available-now.aspx
I jsut wrote this class using the WPF DispatcherTimer but you can swap the dispatcher for any timer that supports changing when it's woken from sleep state.
The class is constructed with a fixed time step and supprts Start/Stop/Reset, Start/Stop/Start works like a resume operation. The timer is like a stopwatch in that regard.
A clock implementation would simply create the class with a interval of 1 second and listen to the event. Be wary though that this is a real-time clock, if the tick event takes longer than the interval to finish you'll notice that the clock will try and catch up to real-time this will cause a burst of tick events being raised.
public class FixedStepDispatcherTimer
{
/// <summary>
/// Occurs when the timer interval has elapsed.
/// </summary>
public event EventHandler Tick;
DispatcherTimer timer;
public bool IsRunning { get { return timer.IsEnabled; } }
long step, nextTick, n;
public TimeSpan Elapsed { get { return new TimeSpan(n * step); } }
public FixedStepDispatcherTimer(TimeSpan interval)
{
if (interval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException("interval");
}
this.timer = new DispatcherTimer();
this.timer.Tick += new EventHandler(OnTimerTick);
this.step = interval.Ticks;
}
TimeSpan GetTimerInterval()
{
var interval = nextTick - DateTime.Now.Ticks;
if (interval > 0)
{
return new TimeSpan(interval);
}
return TimeSpan.Zero; // yield
}
void OnTimerTick(object sender, EventArgs e)
{
if (DateTime.Now.Ticks >= nextTick)
{
n++;
if (Tick != null)
{
Tick(this, EventArgs.Empty);
}
nextTick += step;
}
var interval = GetTimerInterval();
Trace.WriteLine(interval);
timer.Interval = interval;
}
public void Reset()
{
n = 0;
nextTick = 0;
}
public void Start()
{
var now = DateTime.Now.Ticks;
nextTick = now + (step - (nextTick % step));
timer.Interval = GetTimerInterval();
timer.Start();
}
public void Stop()
{
timer.Stop();
nextTick = DateTime.Now.Ticks % step;
}
}
Create a method or put this code where you want the timer to start:
int time = 60 - DateTime.Now.Second; // Gets seconds to next minute
refreshTimer.Interval = time * 1000;
refreshTimer.Start();
And then on your tick event set the interval to 60000:
private void refreshTimer_Tick(object sender, EventArgs e)
{
refreshTimer.Interval = 60000; // Sets interval to 60 seconds
// Insert Refresh logic
}
By making use of ReactiveExtensions you could use the following code if you were interested in doing something as simple as printing to the console.
using System;
using System.Reactive.Linq;
namespace ConsoleApplicationExample
{
class Program
{
static void Main()
{
Observable.Interval(TimeSpan.FromMinutes(1))
.Subscribe(_ =>
{
Console.WriteLine(DateTime.Now.ToString());
});
Console.WriteLine(DateTime.Now.ToString());
Console.ReadLine();
}
}
}
Running a bit of code to see if the minute has changed once per second should not require much CPU time, and should be acceptable.
What about Quartz.NET? I think its a good framework to do timed actions.
You could set up two timers. An initial short interval timer (perhaps to fire every second, but dependent on how presice the second timer must fire on the minute).
You would fire the short interval timer only until the desired start time of the main interval timer is reached. Once the initial time is reached, the second main interval timer can be activated, and the short interval timer can be deactivated.
void StartTimer()
{
shortIntervalTimer.Interval = 1000;
mainIntervalTimer.Interval = 60000;
shortIntervalTimer.Tick +=
new System.EventHandler(this.shortIntervalTimer_Tick);
mainIntervalTimer.Tick +=
new System.EventHandler(mainIntervalTimer_Tick);
shortIntervalTimer.Start();
}
private void shortIntervalTimer_Tick(object sender, System.EventArgs e)
{
if (DateTime.Now.Second == 0)
{
mainIntervalTimer.Start();
shortIntervalTimer.Stop();
}
}
private void mainIntervalTimer_Tick(object sender, System.EventArgs e)
{
// do what you need here //
}
Alternatively, you could sleep to pause execution until it times out which should be close to your desired time. This will only wake the computer when the sleep finishes so it'll save you CPU time and let the CPU power down between processing events.
This has the advantage of modifying the timeout so that it will not drift.
int timeout = 0;
while (true) {
timeout = (60 - DateTime.Now.Seconds) * 1000 - DateTime.Now.Millisecond;
Thread.Sleep(timeout);
// do your stuff here
}
Use a timer set to run every second (or millisecond, whatever your accuracy threshold is), and then code the method to run your functionality if and only if the current time is within that threshold past the "on the minute" point.
What I'm using for scheduled tasks is a System.Threading.Timer(System.Threading.TimerCallback, object, int, int) with the callback set to the code I want to execute based on the interval which is supplied in milliseconds for the period value.
What about a combination of aquinas' answer and 'polling': (apologies for the mixture of languages)
def waitForNearlyAMinute:
secsNow = DateTime.Now.Second;
waitFor = 55 - secsNow;
setupTimer(waitFor, pollForMinuteEdge)
def pollForMinuteEdge:
if (DateTime.Now.Second == 0):
print "Hello, World!";
waitForNearlyAMinute();
else:
setupTimer(0.5, pollForMinuteEdge)
I have a solution based on Environment.TickCount
static void Main(string[] args)
{
//constatnt total miliseconds to one minute
const Int32 minuteMilisecond = 60 * 1000;
//get actual datetime
DateTime actualDateTime = DateTime.UtcNow;
//compenzation to one minute
Int32 nexTimer = Environment.TickCount + ((59 - actualDateTime.Second) * 1000) + (999 - actualDateTime.Millisecond);
//random fuction to simulate different delays on thread
Random rnd = new Random();
//main loop
while (true)
{
if (Environment.TickCount > nexTimer)
{
nexTimer += minuteMilisecond;
//execute your code here every minute
Console.WriteLine($"actual DateTime: {DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff")}");
}
//random sleep between 100 - 200 ms
Thread.Sleep(rnd.Next(100, 200));
}
}

How can I raise an event every hour (or specific time interval each hour) in .NET?

I'm working on a little web crawler that will run in the system tray and crawl a web site every hour on the hour.
What is the best way to get .NET to raise an event every hour or some other interval to perform some task. For example I want to run an event every 20 minutes based on the time. The event would be raised at:
00:20
00:40
01:00
01:20
01:40
and so on. The best way I can think of to do this is by creating a loop on a thread, that constantly checks if the time is divisible by a given interval and raises a callback event if the time is reached. I feel like there has got to be a better way.
I'd use a Timer but I'd prefer something that follows a "schedule" that runs on the hour or something along those lines.
Without setting up my application in the windows task scheduler is this possible?
UPDATE:
I'm adding my algorithm for calculating the time interval for a timer. This method takes a "minute" parameter, which is what time the timer should trigger a tick. For example, if the "minute" parameter is 20, then the timer will tick at the intervals in the timetable above.
int CalculateTimerInterval(int minute)
{
if (minute <= 0)
minute = 60;
DateTime now = DateTime.Now;
DateTime future = now.AddMinutes((minute - (now.Minute % minute))).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
TimeSpan interval = future - now;
return (int)interval.TotalMilliseconds;
}
This code is used as follows:
static System.Windows.Forms.Timer t;
const int CHECK_INTERVAL = 20;
static void Main()
{
t = new System.Windows.Forms.Timer();
t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
t.Tick += new EventHandler(t_Tick);
t.Start();
}
static void t_Tick(object sender, EventArgs e)
{
t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
}
System.Timers.Timer. If you want to run at specific times of the day, you will need to figure out how long it is until the next time and set that as your interval.
This is just the basic idea. Depending on how precise you need to be you can do more.
int minutes = DateTime.Now.Minute;
int adjust = 10 - (minutes % 10);
timer.Interval = adjust * 60 * 1000;
You may find help from Quartz.net http://quartznet.sourceforge.net/
Here is an example of a lightweight system using thread timing and an asynch call.
I know there are some downsides, but I like using this instead of a timer when kicking off a long running process (like schedualed backend services). Since it runs inline in the timer thread, you don't have to worry about it getting kicked off again before the the original call has finished. This could be extended quite a bit to make it use an array of datetimes as the trigger times or add some more abilities to it. I am sure some of you guys out there know some better ways.
public Form1()
{
InitializeComponent();
//some fake data, obviously you would have your own.
DateTime someStart = DateTime.Now.AddMinutes(1);
TimeSpan someInterval = TimeSpan.FromMinutes(2);
//sample call
StartTimer(someStart,someInterval,doSomething);
}
//just a fake function to call
private bool doSomething()
{
DialogResult keepGoing = MessageBox.Show("Hey, I did something! Keep Going?","Something!",MessageBoxButtons.YesNo);
return (keepGoing == DialogResult.Yes);
}
//The following is the actual guts.. and can be transplanted to an actual class.
private delegate void voidFunc<P1,P2,P3>(P1 p1,P2 p2,P3 p3);
public void StartTimer(DateTime startTime, TimeSpan interval, Func<bool> action)
{
voidFunc<DateTime,TimeSpan,Func<bool>> Timer = TimedThread;
Timer.BeginInvoke(startTime,interval,action,null,null);
}
private void TimedThread(DateTime startTime, TimeSpan interval, Func<bool> action)
{
bool keepRunning = true;
DateTime NextExecute = startTime;
while(keepRunning)
{
if (DateTime.Now > NextExecute)
{
keepRunning = action.Invoke();
NextExecute = NextExecute.Add(interval);
}
//could parameterize resolution.
Thread.Sleep(1000);
}
}
Another strategy for this would be to record the LAST TIME that the process was run and determine if your desired interval has elapsed since that time. In this strategy, you would code your event to fire if the elapsed time is equal to OR GREATER THAN the desired interval. In this way you can handle instances where long intervals (once per day, for example) could be missed if the computer were to be down for some reason.
So for example:
lastRunDateTime = 5/2/2009 at 8pm
I want to run my process every 24 hours
On a timer event, check whether 24 hours OR MORE passed since the last time the process was run.
If yes, run the process, update lastRunDateTime by adding the desired interval to it (24 hours in this case, but whatever you need it to be)
Obviously, for this to recover after the system has gone down, you will need to store lastRunDateTime in a file or database somewhere so the program could pick up where it left off on recovery.
System.Windows.Forms.Timer (or System.Timers.Timer)
but since now you say you don't want to use Timers, you can run a lightweight wait process on another thread (check time, sleep a few seconds, check time again...) or make a component that raises an event (using a lightweight wait process) on certain scheduled times or intervals
The following should do the trick.
static void Main(string[] Args)
{
try
{
MainAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static async Task MainAsync()
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
// Start the timed event here
StartAsync(tokenSource.Token);
Console.ReadKey();
tokenSource.Cancel();
tokenSource.Dispose();
}
public Task StartAsync(CancellationToken cancellationToken)
{
var nextRunTime = new DateTime();
switch (DateTime.Now.AddSeconds(1) < DateTime.Today.AddHours(12)) // add a second to current time to account for time needed to setup the task.
{
case true:
nextRunTime = DateTime.Today.AddHours(12); // Run at midday today.
break;
case false:
nextRunTime = DateTime.Today.AddDays(1).AddHours(12); // Run at midday tomorrow.
break;
}
var firstInterval = nextRunTime.Subtract(DateTime.Now);
Action action = () =>
{
// Run the task at the first interval, then run the task again at midday every day.
_timer = new Timer(
EventMethod,
null,
firstInterval,
DateTime.Today.AddDays(1).AddHours(12).Subtract(DateTime.Now)
);
};
// no need to await this call here because this task is scheduled to run later.
Task.Run(action);
return Task.CompletedTask;
}
private async void EventMethod(object state)
{
// do work
}
My goal is to run an import around 03:00 every night.
Here's my approach, using System.Timers.Timer:
private Timer _timer;
private Int32 _hours = 0;
private Int32 _runAt = 3;
protected override void OnStart(string[] args)
{
_hours = (24 - (DateTime.Now.Hour + 1)) + _runAt;
_timer = new Timer();
_timer.Interval = _hours * 60 * 60 * 1000;
_timer.Elapsed += new ElapsedEventHandler(Tick);
_timer.Start();
}
void Tick(object sender, ElapsedEventArgs e)
{
if (_hours != 24)
{
_hours = 24;
_timer.Interval = _hours * 60 * 60 * 1000;
}
RunImport();
}

Categories