i have an application (.Net Framework 2.0!) where you can enter operations which will be executed at a given time.
Now i want to create a process which runs in background and does nothing else then waiting till the given time is reached and call the operation to run. The application should run things like making backup of specific parts of the computer, start updates, run batches, ... The backgroundworker will run over several month doing nothing else.
Using the code below would work but it seems a bit ugly. Is there any better solution?
while(true && !applicationClosing)
{
List<ExecProcess> processList = LoadStoredProcesses();
List<ExecProcess> spawnedProcesses = new List<ExecProcess>();
DateTime actualTime = DateTime.Now();
foreach(ExecProcess ep in processList)
{
if(ep.ExecutionTime < actualTime)
{
ep.Execute();
spawnedProcesses.Add(ep);
}
}
RemoveSpawnedProcesses(spawnedProcesses);
Thread.Sleep(1000);
}
Thank you verry much.
I would suggest using a Windows service which implements a timer that fires an event every n seconds. You can pickup your tasks from wherever you want, and queue them internally in the service and fire at given times. Just check the timestamps within the _executeTimer_Elapsed method. This is only a small sample, but it should be enough to get you started.
public class MyService : ServiceBase
{
private Timer _executeTimer;
private bool _isRunning;
public MyService()
{
_executeTimer = new Timer();
_executeTimer.Interval = 1000 * 60; // 1 minute
_executeTimer.Elapsed += _executeTimer_Elapsed;
_executeTimer.Start();
}
private void _executeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!_isRunning) return; // logic already running, skip out.
try
{
_isRunning = true; // set timer event running.
// perform some logic.
}
catch (Exception ex)
{
// perform some error handling. You should be aware of which
// exceptions you can handle and which you can't handle.
// Blanket handling Exception is not recommended.
throw;
}
finally
{
_isRunning = false; // set timer event finished.
}
}
protected override void OnStart(string[] args)
{
// perform some startup initialization here.
_executeTimer.Start();
}
protected override void OnStop()
{
// perform shutdown logic here.
_executeTimer.Stop();
}
}
Related
using System.Timers;
void CreateTimer()
{
myTimerObject = new Timer(5000);
myTimerObject.AutoReset = false;
myTimerObject.Elapsed += MyEventOnElapsed;
myTimerObject.Start();
}
void MyEventOnElapsed(object sender, ElapsedEventArgs e)
{
lock(aLockObject)
{
myTimerObject.Stop();
// Perform actions that can exceed the interval time set in the timer
myTimerObject.Start();
}
}
void MethodTrigerredToStopTimer()
{
lock(aLockObject)
{
myTimerObject.Stop();
}
}
In the above code, my elapsed event (MyEventOnElapsed) is going to take a while to complete and hence I had to use timer start and stop as part of that method. When MethodTriggeredToStopTimer is triggered, let's assume the code is at MyEventOnElapsed and reached lock, however the thread at MethodTriggerredToStopTimer wins the race and manages to get the lock, my timer will be stopped by the MethodTriggeredToStopTimer(). However once the lock is released, the execution pointer waiting at the lock(aLockObject) in MyEventOnElapsed will continue start/stop the timer indefinitely. How to handle timers in this situation?
If you want a definitive way to stop via a method call or trigger when managing a loop like this, you will need to maintain some boolean isStopped or similar:
boolean isStopped = false;
void CreateTimer()
{
myTimerObject = new Timer(5000);
myTimerObject.AutoReset = false;
myTimerObject.Elapsed += MyEventOnElapsed;
myTimerObject.Start();
}
void MyEventOnElapsed(object sender, ElapsedEventArgs e)
{
lock(aLockObject)
{
if (isStopped)
return;
myTimerObject.Stop();
// Perform actions that can exceed the interval time set in the timer
myTimerObject.Start();
}
}
void MethodTrigerredToStopTimer()
{
lock(aLockObject)
{
isStopped = true;
myTimerObject.Stop();
}
}
This would all be much neater in a nice little wrapper class, with the state variable named something like Enabled to avoid confusion with the naming similarities to the Timer's start and stop methods. Additionally, I would take a look at building this loop using Task, Task.Delay, and CancellationToken as well if you end up needing cross-platform support or want to handle things like cancellation during your long-running operation.
One option would be to ask the timer whether the Timer is enabled before stopping (disabling) it. That way, if MethodTrigerredToStopTimer is called it will realise it has been stopped on purpose, and not start it up again.
void MyEventOnElapsed(object sender, ElapsedEventArgs e)
{
lock(aLockObject)
{
if (myTimerObject.Enabled)
{ try
{
myTimerObject.Stop();
// Perform actions that can exceed the interval time set in the timer
}
finally
{
myTimerObject.Start();
}
}
}
}
The try finally is helpful in ensuring that the timer is restarted by the time you exit the lock (even if an exception is thrown).
I ran into problem while executing windows service in C#, not sure but probably due to the deadlock between the thread listening to the event handler and the normal code flow. The service just hangs when an event is listened and back to the normal flow where the Thread.Sleep is executed. The windows service goes into sleep mode normally in the first time, and in the next time the duration gets automatically doubled and thereafter it never wakes up and the service moves into a "Deadlock" mode.
There is one global variable in the below snippet controller.DeviceState, which is used both by the event listener and the main flow. All the exceptions are handled. Please let me know why the code just goes into "never waking sleep mode"
Below is the general code flow:
Main service
public partial class MainService : ServiceBase
{
protected override void OnStart(string[] args)
{
try
{
ThreadStart start = new ThreadStart(MainProcess);
Thread mainProcessThread = new Thread(start);
// set flag to indicate worker thread is active
serviceStarted = true;
// start threads
mainProcessThread.Start();
}
catch(Exception ex)
{
//catch exception
}
}
string testVariable = "YES";
//Event handler
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.IO.Ports.SerialPort sp = (System.IO.Ports.SerialPort)sender;
string s = sp.ReadExisting();
if (s == "Wifi")
{
testVariable = "NO";
}
}
private void MainProcess()
{
try
{
int i = 0;
System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort("COM4");
sp.Open();
sp.DataReceived += serialPort1_DataReceived;
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.Parity = System.IO.Ports.Parity.None;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Handshake = System.IO.Ports.Handshake.None;
sp.DtrEnable = true;
while (testVariable == "YES")
{
i++;
//Sleep until the testVariable is set to NO
Thread.Sleep(5000);
}
}
catch(Exception ex)
{
//catch exception here
}
}
protected override void OnStop()
{
}
}
I think now i actually figured out what was causing the deadlock. In order to replicate, I increased the sleep time to 20 seconds and ran. What i found is, when a message is retrieved from the event handler during that period(sleep time) then the whole application goes to the hang mode, I don't understand the actual reason, but IMO the Thread.Sleep would also prevent any event handlers to listen and if it does then whole system would go to the "DEAD" mode.
To fix it, I initialized in the MainClass,
private static AutoResetEvent event_1 = new AutoResetEvent(true);
And added event_1.Set(); prior to Thread.Sleep as below:
while (testVariable == "YES")
{
Common.WriteLogIntoFile("i", i.ToString(), "FaxWorker()");
i++;
event_1.Set();
Thread.Sleep(20000);
}
But I don't know how it fixes it, if anyone knows it please let me know.
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();
}
}
I have a C# program that is constantly checking for new additions to an online DB. I have this code to have it check every 10 seconds
static void Main(string[] args)
{
boolean run = true;
while (run)
{
DBConnect Db = new DBConnect();
// do amazing awesome mind blowing cool stuff
Db.closeConnection();
// wait for 10 seconds
int wait = 10 * 1000;
System.Threading.Thread.Sleep(wait);
}
}
i have error reporting that posts to the DB and if a major error occurs the program shuts down. Outside of the specific errors within my function, is this method secure and efficient?
You should rewrite your program as a windows service, that way you do not need to rely on a user to be logged for your program to run.
If you do go with the service route, I would swap out the infinite loop for a timer.
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
int wait = 10 * 1000;
timer = new Timer(wait);
timer.Elapsed += timer_Elapsed;
// We don't want the timer to start ticking again till we tell it to.
timer.AutoReset = false;
}
private System.Timers.Timer timer;
protected override void OnStart(string[] args)
{
timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
DBConnect Db = new DBConnect())
try
{
// do amazing awesome mind blowing cool stuff
}
finally
{
Db.closeConnection(); //We put this in a finally block so it will still happen, even if an exception is thrown.
}
timer.Start();
}
catch(SomeNonCriticalException ex)
{
MyExecptionLogger.Log(ex, Level.Waring); //Log the exception so you know what went wrong
timer.Start(); //Start the timer for the next loop
}
catch(Exception ex)
{
MyExecptionLogger.Log(ex, Level.Critical); //Log the exception so you know what went wrong
this.Stop(); //Stop the service
}
}
protected override void OnStop()
{
timer.Stop();
}
}
Write it as a console program without the wait and set up a scheduled task to run it periodically. You want to run it every 10 seconds? Every minute? Just change the scheduled task.
You can use the Task Scheduler GUI, or the schtasks command line tool.
See Programs are not cats.
I would setup a windows service and use SqlDependency http://msdn.microsoft.com/en-CA/library/a52dhwx7(v=vs.80).aspx. That way when a change (which you specify) occurs in the database, it will trigger the OnChange event which you specify to do whatever it is you need to do. See the link for implementation details.
I have a service written in C# (.NET 1.1) and want it to perform some cleanup actions at midnight every night. I have to keep all code contained within the service, so what's the easiest way to accomplish this? Use of Thread.Sleep() and checking for the time rolling over?
I wouldn't use Thread.Sleep(). Either use a scheduled task (as others have mentioned), or set up a timer inside your service, which fires periodically (every 10 minutes for example) and check if the date changed since the last run:
private Timer _timer;
private DateTime _lastRun = DateTime.Now.AddDays(-1);
protected override void OnStart(string[] args)
{
_timer = new Timer(10 * 60 * 1000); // every 10 minutes
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
//...
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// ignore the time, just compare the date
if (_lastRun.Date < DateTime.Now.Date)
{
// stop the timer while we are running the cleanup task
_timer.Stop();
//
// do cleanup stuff
//
_lastRun = DateTime.Now;
_timer.Start();
}
}
Check out Quartz.NET. You can use it within a Windows service. It allows you to run a job based on a configured schedule, and it even supports a simple "cron job" syntax. I've had a lot of success with it.
Here's a quick example of its usage:
// Instantiate the Quartz.NET scheduler
var schedulerFactory = new StdSchedulerFactory();
var scheduler = schedulerFactory.GetScheduler();
// Instantiate the JobDetail object passing in the type of your
// custom job class. Your class merely needs to implement a simple
// interface with a single method called "Execute".
var job = new JobDetail("job1", "group1", typeof(MyJobClass));
// Instantiate a trigger using the basic cron syntax.
// This tells it to run at 1AM every Monday - Friday.
var trigger = new CronTrigger(
"trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");
// Add the job to the scheduler
scheduler.AddJob(job, true);
scheduler.ScheduleJob(trigger);
A daily task? Sounds like it should just be a scheduled task (control panel) - no need for a service here.
Does it have to be an actual service? Can you just use the built in scheduled tasks in the windows control panel.
The way I accomplish this is with a timer.
Run a server timer, have it check the Hour/Minute every 60 seconds.
If it's the right Hour/Minute, then run your process.
I actually have this abstracted out into a base class I call OnceADayRunner.
Let me clean up the code a bit and I'll post it here.
private void OnceADayRunnerTimer_Elapsed(object sender, ElapsedEventArgs e)
{
using (NDC.Push(GetType().Name))
{
try
{
log.DebugFormat("Checking if it's time to process at: {0}", e.SignalTime);
log.DebugFormat("IsTestMode: {0}", IsTestMode);
if ((e.SignalTime.Minute == MinuteToCheck && e.SignalTime.Hour == HourToCheck) || IsTestMode)
{
log.InfoFormat("Processing at: Hour = {0} - Minute = {1}", e.SignalTime.Hour, e.SignalTime.Minute);
OnceADayTimer.Enabled = false;
OnceADayMethod();
OnceADayTimer.Enabled = true;
IsTestMode = false;
}
else
{
log.DebugFormat("Not correct time at: Hour = {0} - Minute = {1}", e.SignalTime.Hour, e.SignalTime.Minute);
}
}
catch (Exception ex)
{
OnceADayTimer.Enabled = true;
log.Error(ex.ToString());
}
OnceADayTimer.Start();
}
}
The beef of the method is in the e.SignalTime.Minute/Hour check.
There are hooks in there for testing, etc. but this is what your elapsed timer could look like to make it all work.
As others already wrote, a timer is the best option in the scenario you described.
Depending on your exact requirements, checking the current time every minute may not be necessary.
If you do not need to perform the action exactly at midnight, but just within one hour after midnight, you can go for Martin's approach of only checking if the date has changed.
If the reason you want to perform your action at midnight is that you expect a low workload on your computer, better take care: The same assumption is often made by others, and suddenly you have 100 cleanup actions kicking off between 0:00 and 0:01 a.m.
In that case you should consider starting your cleanup at a different time. I usually do those things not at clock hour, but at half hours (1.30 a.m. being my personal preference)
I would suggest that you use a timer, but set it to check every 45 seconds, not minute. Otherwise you can run into situations where with heavy load, the check for a particular minute is missed, because between the time the timer triggers and the time your code runs and checks the current time, you might have missed the target minute.
You can also try the TaskSchedulerLibrary here http://visualstudiogallery.msdn.microsoft.com/a4a4f042-ffd3-42f2-a689-290ec13011f8
Implement the abstract class AbstractScheduledTask and call the ScheduleUtilityFactory.AddScheduleTaskToBatch static method
For those that found the above solutions not working, it's because you may have a this inside your class, which implies an extension method which, as the error message says, only makes sense on a non-generic static class. Your class isn't static. This doesn't seem to be something that makes sense as an extension method, since it's acting on the instance in question, so remove the this.
Try this:
public partial class Service : ServiceBase
{
private Timer timer;
public Service()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
SetTimer();
}
private void SetTimer()
{
if (timer == null)
{
timer = new Timer();
timer.AutoReset = true;
timer.Interval = 60000 * Convert.ToDouble(ConfigurationManager.AppSettings["IntervalMinutes"]);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
}
private void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
//Do some thing logic here
}
protected override void OnStop()
{
// disposed all service objects
}
}