Hi I have a Windows Service in C#. Inside my windows service I need to run a process on determined hours hh:mm:ss.
For example I need to run the process at:
09:50 hrs
11.45 hrs
15:15 hrs
22:05 hrs
(These hours can change, so Im thinking in storing those in a XML file)
Right now I used to have a timer with interval of 6 hours, so every 6 hours the process was executed. Now the requirement is other. I need to run on a specific schedule.
Also I was requested to add an option that the windows service can run the process on every X hours and Y minutes.
Any clue how can I code that?
Thanks.
Another good way could be using Quartz
Quartz.NET
Quartz.NET is a full-featured, open source job scheduling system that
can be used from smallest apps to large scale enterprise systems.
Job Scheduling in Quartz
Jobs are scheduled to run when a given Trigger occurs.
Triggers can be created with nearly any combination of the following
directives:
at a certain time of day (to the millisecond)
on certain days of the week
on certain days of the month
on certain days of the year
not on certain days listed within a registered Calendar (such as business holidays)
repeated a specific number of times
repeated until a specific time/date
repeated indefinitely
repeated with a delay interval
Why reinvent the wheel? Just use Windows Task Scheduler to run your process.
If you want to setup the task scheduler from your code, you can use the Managed Task Scheduler Wrapper
Related
I have a requirement wherein I have to Run a SSIS Job for only 2 Hours.
Now this Job Could be started anytime of the Day, but after 2 Hours It should Automatically Stop.
Can this be achieved using SSIS?
Is there any way I could go ahead with this?
I am having a problem with Quartz.NET to create a job on a system. The Quartz is sleeping 23:50 and waking up 7:26 AM.
I have a job scheduled for 00:00
System Information: 1 - .NET Application 2 - IIS Server
Application log:
enter image description here
I personally just use quartz job calculator the set job execute time when server is up
https://www.freeformatter.com/cron-expression-generator-quartz.html
and have the ability to manually start them later if needed.
You can also create a job that automates this. For example it runs every 10m that checks if some other job has to be run. It does so by checking when it was last ran, and if it is ran it will set a sync lock - a simple local time, so you can check if some amount of time ex: 1 day has passed.
I did setup a Cron Job that runs every hour in C# as a service; it sweeps files from a folder and dumps them onto ftp, however during busy hours one hour is not enough, how do we make sure that the previous hours job is completed first before starting this hours job.
There's an attribute called [DisallowConcurrentExecution] which you can use. Refer to the documentation pertaining to the exact version of Quartz Scheduler.NET that you are using.
You can find more info about it at https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/more-about-jobs.html
I have developed a .NET program for a SCADA solution to control a heavy machine but I have some problem related with time management on the application and I am looking for some wise advisory.
I use a Winforms timer to check regularly and record the values of some variables related with the process being controlled. The application spends about 40h turned on without interruptions. At the beggining the timer does it's job, at a 5 minutes time interval, it records the values to the database. But at the end of the 40 hours, the same timer without changing it's configuration is invoked only 1 time per hour.
So my question basically is "What's the best way to ensure a certain code is run on fixed periodic intervals in a program developed in C#?" I don't really need a pure real time solution, just ensure a function is called always in fixed time intervals. But those intervals are not quite critical, we are talking about 5 minutes length. Is not important how long it takes to run the code but it is important that the code is always executed on the same period of time.
Is it better option to run the application as a service rather than a regular user-space program? Is it better option to develop in C++ the "time critical" part and communicate with the C# code via sockets or so?
At the beggining the timer does it's job, at a 5 minutes time interval...
I don't really need a pure real time solution...
Windows Task Scheduler is built for this in mind. With it you can simply have it run an .exe of your choosing with optional arguments. If the schedules are static you are arguably better off setting up the schedule in the TS UI or if complex, via the COM API.
Much better than having a another process hanging about counting down when TS already does it.
I have to create an app that will read in some info from a db, process the data, write changes back to the db, and then send an email with these changes to some users or groups. I will be writing this in c#, and this process must be run once a week at a particular time. This will be running on a Windows 2008 Server.
In the past, I would always go the route of creating a windows service with a timer and setting the time/day for it to be run in the app.config file so that it can be changed and only have to be restarted to catch the update.
Recently, though, I have seen blog posts and such that recommend writing a console application and then using a scheduled task to execute it.
I have read many posts talking to this very issue, but have not seen a definitive answer about which process is better.
What do any of you think?
Thanks for any thoughts.
If it is a one per week application, why waste the resources for it to be running in the background for the rest of the week.
A console application seems much more appropriate.
The typical rule of thumb that I use is something along these lines. First I ask a few questions.
Frequency of Execution
Frequency of changes to #1
Triggering Mechanism
Basically from here if the frequency of execution is daily or less frequent I'll almost always lean towards a scheduled task. Then looking at the frequency for changes, if there is a high demand for schedule changes, I'll also try to lean towards scheduled tasks, to allow no-coding changes for schedule changes. lastly if there is ever a thought of a trigger other than time, then I'll lean towards windows services to help "future proof" an application. Say for example the requirement changes to be run every time a user drops a file in X folder.
The basic rule I follow is: if you need to be running continuously because events of interest can happen at any time, use a service (or daemon in UNIX).
If you just want to periodically do something, use a scheduled task (or cron).
The clincher here is your phrase "must be run once a week at a particular time" - go for a scheduled task.
If you have only one application and you need it to run once a week may be scheduler will be good as there is no need to have separate service and process running on the system which will be idle most of the time.