scheduling a task c# - c#

I have a C# console application. I would like to run that every hour of the day. This application is not intended to run on my local machine but on other users machine as a background process. I don't want the user to take the trouble of setting a task manually using task scheduler. How can I achieve the same for him ?

You can set it up to run on a timer with an interval of 1 hour.
See this link for examples
How do you add a timer to a C# console application
You could also install it as a windows service on the user's machine.

You can also use the Quartz.net scheduling service; in its simplest form it would install as a Windows Service and the schedule is configured in an XML file. You can then have it call your code at the scheduled time.
I kept some notes... not sure if this helps:
The Quartz.Net Guide I wish I had
Quartz.Net is very full featured, as far as I can read. If you use a scheduler so that you can defer tasks, or decouple a front-end from some rather long back-end processing, it can be done quite easily.
The large confusion for me was that Quartz can be used in many ways. You can embed it in your own app or service, etc. etc. The other confusion was that the official Tutorial could use some updating so that the code samples can work with the current version. Finally, the configuration seems pretty foreign for a .net user; I'm used to doing something like:
var myObject = new SuperUsefulClass();
myObject.ImportantSetting = SettingEnumeration.SomeValueThatIntellisenseSuggests;
myObject.OtherSetting = OtherSettingEnumeration.SomeValueThatIntellisenseSuggests;
Usually I like to explore a new class in LINQPad. I'll instantiate it as above, and play around with it until I am comfortable with using it in my project.
The configuration of Quartz.Net was about as foreign to me as imaginable. But that is because I overlooked one of the best pre-built options for using Quartz.Net, and I overlooked one of the best tutorials on Quartz.
For my use case, deferring tasks, I eventually realized the easiest way to use Quartz was with the pre-built Windows Service. To do that I simply modified an example configuration file, and I chose to persist all the jobs, job metadata, etc. to a SQL Server Database.
When it comes time for the job to be executed, the Quartz Windows Service will call the designated method (which of course implements the IJob interface). In order for the method to be called, your .dll or .exe ("assembly" in .net parlance) must be in the same folder where the service (Quartz.Service.exe) was installed. It then magically calls the method in your assembly. Of course if your method reads external files, those must also be copied to the service folder, or reachable somehow by the assembly.
After nearly giving up, I ran across Tarun Arora's tutorial. It is really excellent and since I cannot improve on it, I'll just link to it.
Install Quartz.Net as a windows service and Test installation
Quartz.Net Writing your first Hello World Job
Basically:
Tweak the quartz.config file (similar to the code above)
Install the Quartz.Net service (Quartz.Service.exe) as Administrator
Put assemblies (.exe or .dll) of code you want to execute, in the same folder as Quartz.Service.exe
Start the service
Make scheduling calls. i.e. your assembly code will make calls to the Quartz Service, which then persists and executes them on its
local system.

Create a service application: http://msdn.microsoft.com/en-us/library/zt39148a.aspx

Related

C# - Scheduling multiple operations in windows service without timer

I need to write a windows service which performs several operations as follows:
Fetching data from api and dump it to db - to be performed every hour
Consolidation - to be performed every 6 hours
Summary - to be performed once a day at 01:00
I don't want to use multiple timers or no timer at all. Please suggest best way to schedule these tasks.
There are multiple ways to do this. If you insist on using a windows service, you could look into Hangfire. The cost for using this is that you'll need to add a database that can store the information about the jobs.
Check the following link for Hangfire: https://www.hangfire.io/
You can also use the native Windows Scheduler for scheduling jobs.
Instead of creating a windows service you'll create a console app or something similar and use the Windows Scheduler for when it should execute.
If you don't want to use the native functionality I recommend using a nuget package like this one; https://www.quartz-scheduler.net/
All the above was found with a little bit of google magic and knowing the windows operating system..

C# scheduler tools

I’m loath to ask another scheduler question here, I’ve read through dozens, but it’s still not clear to me what tools would best fit my need. I have three requirements for a reporting app:
User invoked
fixed scheduled
user scheduled.
I have an ASP.NET forms app to cover #1 and a C# console app to handle #2 but now #3 has been added to the mix.
So for the user scheduled reports I need to:
Present the user with a schedule selector and save their selection (into SQL Server?)
Have an app that checks the database for jobs to run/schedule
App to run the query and format the report
I suppose the latter two could be a single app but I’ve read it’s hard to debug service apps so keeping them separate may be good. I don’t know what parts of my requirements are met by Quartz.net and I’ve seen separate GUI tools (DayPilot) and backend (Task Manager API, CodePlex taskscheduler) mentioned. Not having used any of these I’m hoping to minimize my false starts.
If you require job scheduler try using hangfire.io
If you have SQL Server, then you should use SQL Server Reporting Services, which does all three.

How to have periodical events in a C# MVC web app?

I have a web app that I'm writing right now that is supposed to have "periodical events". For instance, at midnight, the web app should calculate "scores" for all users. I want this done only once during the day.
Is there a way that I can automate this, so it runs automatically at midnight (or whatever hour I choose)?
I don't like the idea of creating a separate script (VBS) to do this, as the calculation would depend on a lot of business logic of the app. I was thinking to put it into a separate Class Library, so it can use the web app logic (which is also in a class library), but is this the best way to go about it?
I also don't like the idea of using the Session_Start() event in the Global.asax to trigger the event by checking the hour manually. There must be some easier way - especially because down the road I expect there will be a lot more of periodical events - some may have to be triggered every fifteen minutes, for example ...
Thanks a lot for any help you can give me.
You should not do this in the web app itself. You are correct to put the business logic in a separate library. Once you have done this, you can use the business logic from anywhere, and therefore, a good solution would be to create a console application that does the nightly jobs, and invoke the console application from Windows Task Scheduler. IIS is not suitable as a host for periodical events.
I guess you are missing the point of separation of concern. Whatever you are asking is a job of a service. You need to develop a separate application as Windows Service that will do all your calculation and to be triggered by any scheduler even Windows Task Scheduler would do. This is what basically done on large scale applications.
Yeah... again awesome "change your architecture and hosting environment so that my answer can be relevant" responses.
Doing what you ask is actually quite easy, take a look at this article: http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc
This is a job for a windows service or scheduled task. A web application responds to HTTP requests. Essentially the service's job would be to wake up, run the appropriate calculations and write back to the database. Once in the database, your web application can use the newly calculated values.
Here is some information on windows services: http://msdn.microsoft.com/en-us/library/d56de412.aspx

Hints and tips for a Windows service I am creating in C# and Quartz.NET

I have a project ongoing at the moment which is create a Windows Service that essentially moves files around multiple paths. A job may be to, every 60 seconds, get all files matching a regular expression from an FTP server and transfer them to a Network Path, and so on. These jobs are stored in an SQL database.
Currently, the service takes the form of a console application, for ease of development. Jobs are added using an ASP.NET page, and can be editted using another ASP.NET page.
I have some issues though, some relating to Quartz.NET and some general issues.
Quartz.NET:
1: This is the biggest issue I have. Seeing as I'm developing the application as a console application for the time being, I'm having to create a new Quartz.NET scheduler on all my files/pages. This is causing multiple confusing errors, but I just don't know how to institate the scheduler in one global file, and access these in my ASP.NET pages (so I can get details into a grid view to edit, for example)
2: My manager would suggested I could look into having multiple 'configurations' inside Quartz.NET. By this, I mean that at any given time, an administrator can change the applications configuration so that only specifically chosen applications run. What'd be the easiest way of doing this in Quartz.NET?
General:
1: One thing that that's crucial in this application is assurance that the file has been moved and it's actually on the target path (after the move the original file is deleted, so it would be disastrous if the file is deleted when it hasn't actually been copied!). I also need to make sure that the files contents match on the initial path, and the target path to give peace of mind that what has been copied is right. I'm currently doing this by MD5 hashing the initial file, copying the file, and before deleting it make sure that the file exists on the server. Then I hash the file on the server and make sure the hashes match up. Is there a simpler way of doing this? I'm concerned that the hashing may put strain on the system.
2: This relates to the above question, but isn't as important as not even my manager has any idea how I'd do this, but I'd love to implement this. An issue would arise if a job is executed when a file is being written to, which may be that a half written file will be transferred, thus making it totally useless, and it would also be bad as the the initial file would be destroyed while it's being written to! Is there a way of checking of this?
As you've discovered, running the Quartz scheduler inside an ASP.NET presents many problems. Check out Marko Lahma's response to your question about running the scheduler inside of an ASP.NET web app:
Quartz.Net scheduler works locally but not on remote host
As far as preventing race conditions between your jobs (eg. trying to delete a file that hasn't actually been copied to the file system yet), what you need to implement is some sort of job-chaining:
http://quartznet.sourceforge.net/faq.html#howtochainjobs
In the past I've used the TriggerListeners and JobListeners to do something similar to what you need. Basically, you register event listeners that wait to execute certain jobs until after another job is completed. It's important that you test out those listeners, and understand what's happening when those events are fired. You can easily find yourself implementing a solution that seems to work fine in development (false positive) and then fails to work in production, without understanding how and when the scheduler does certain things with regards to asynchronous job execution.
Good luck! Schedulers are fun!

What would be the best approach is my application had to pull information from a website every day at 2am

I'm making a small application that is supposed to download info from the web every day at 2am. It will download the information and write the strings to an XML file of my choosing.
Using .NET and C#.
My initial approach was to install a service on the users computer and have that run, but I'm not so sure. I've not even used it so much in the past, only once.
Which is the best (read: time tested :P ) approach to this very common problem.
You can either build your application as a Windows Service, as you mentioned.
Or else it would probably be a better idea to create a normal console application, and launch it automatically at 2.00am with the Windows Task Scheduler.
You can consider both methods as popular and "time-tested".
I would suggest having a console app, which calls data fetching algo in a separate public class (not the main method).
Like Daniel mentioned, run it via Windows Task Scheduler which itself will take care of most scheduling requirements.
This allows the solution to be scaled in the future if need be. E.g. convert into Windows Service, full GUI Winform or even SQL server scheduled tasks etc.

Categories

Resources