I'm using C#.
I think thats a noob question but i'm try anyway.
I have a C# project with methods: X(),Y(),Z().
I'm looking for a task scheduler that run for me every day/every hour my methods (X(),Y(),Z()) at diffrent times.
What is the best way to do that?
Just make a console application that accepts a single parameter (think myapp.exe x) that would call respective function. Then add a number of scheduled tasks to Windows Scheduler and you'll be all set.
The easiest way is to use an existing library. Scott Hanselman had a blog on this a while back http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx.
Note: the article mentions asp.net however the schedulers are mentioned can also be run through console or Windows apps.
I personally use the Fluent scheduler (https://github.com/fluentscheduler/FluentScheduler) which has now been updated to support .net core. It's easy to use, and if you have simple scheduling requirements probably the one I'd recommend.
If you want to run some code every hour, every day or any other clearly known period, then the best option is to create a console application and use Windows Task Scheduler to run this application.
If this period is unclear and depends on some logics or calculations, then the best option is using always-running Windows Services which will run your methods when necessary.
If your application is always run, you can try with reactive extensions:
Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x=>X());
Check here for more infohttps://msdn.microsoft.com/en-us/library/hh242977(v=vs.103).aspx
I would make a windows service, and in the windows service you can call your methods with a timer check. (we did it so in our old company)
Related
I am using c sharp in .NET framework and want to create a scheduled task that runs every day at the same time to check all items in a specified file are due to expire. If they are due to expire the algorithm will send an alert. I have read up on windows task scheduler and was wondering if this is something I could use in this instance or if there is another scheduling instance I could use?
I've seen code that runs an executable program with task scheduler but I haven't seen any that run a particular function.
Any help is appreciated
If your goal is to schedule the task in the C# application itself you could use a framework like Quartz. But this means your application must run all the time. Maybe creating a Windows service would be a good option for that.
If your application should be called by a scheduler I would give the Windows Task Scheudler a try and see if it fullfils all your requirements. Here is a good start on how to use it: https://www.technipages.com/scheduled-task-windows
If you need multiple different task that are all embeded in the same application, I would go with the command line approach suggested by John. For that you could use the Command Line Parser Library or something similar.
My website needs to somehow do something every few minutes.
Essentially I get projects from another website. I then call one of my web pages and update.
Currently I'm making a console app for this but I'm wondering if there is a better way.
Thanks
There are super cool, open source tool exist for creating a scheduled jobs in .NET
It called Quartz.NET.
I think currently is #1 tool for that tasks.
Use windows task scheduler to run the console app your writing at the appropriate times.
Depending on what needs to be done, there are a few options.
This can be achieved by a timer in a windows service.
Using the HttpCache expiration callback feature (not recommended, it is a hack).
Use a Sql job (assuming data is changing).
Building upon #Ph0en1x answer, there already is a Stackoverlow question on how to use Quartz.Net in ASP.Net
In PHP we have cron jobs, where the hosting server automatically picks up and executes a task as per the schedule given.
What would be a good alternative to use for CRON jobs in ASP.NET? I'd like to use a Web service, but that will not work in a shared hosting environment. Any help would be appreciated, and please advise a way to do this specifically in a shared hosting environment.
Try Quartz.NET. It's a decent .NET scheduler which supports CRON expressions, CRON triggers and various other means and methods to schedule tasks to be performed at certain times / intervals.
It even includes a basic Quartz.NET server (Windows Application) that might fit your needs.
Edit:
If you can't run applications or windows services within your shared hosting then perhaps something like "Easy Background Tasks in ASP.NET" will do you? It simulates a Windows Service using HttpRuntime.Cache and removes the need for any external dependancies.
Another option is to use hangfire which says about itself
An easy way to perform background processing in .NET and .NET Core applications. No Windows Service or separate process required.
Backed by persistent storage. Open and free for commercial use.
Requirements (as of November 2019) are
.NET Framework 4.5
Persistent storage, e.g. an SQL database where Hangfire will write 10 tables
Newtonsoft.Json library ≥ 5.0.1
Assuming that you have already some API project which runs as service, you can use hangfire within.
Disclaimer: I am not affiliated in any kind with hangfire, I will just be using it soon.
This is absolutely a hack, but you can simulate a cron service using ASP.NET callbacks. Here is an example of how to do it.
nCron is a .Net implementation of CRON using nCronTabs which are the exact same as cron scheduling
Asp.Net and Web services are typically responsive. They wait and respond to requests. There are hacks to make them look like schedulers but the hacks are ugly beyond belief.
You could use a Windows Service.
Or write an executable and use the Windows Task Scheduler.
I've got a windows service with only two methods - one private method DoWork(), and an exposed method which calls DoWork method. I want to achieve the following:
Windows service runs DoWork() method every 6 hours
An external program can also invoke the exposed method which calls DoWork() method. If the service is already running that method called from the service, DoWork() will again be invoked after the current method ends.
What's the best approach to this problem? Thanks!
An alternative approach would be to make use of a console application which can be scheduled by Windows task scheduler to run every 6 hours. In that case you don't waste resources to keep the Windows service running the entire time but only consume resources when needed.
For your second question: when you take the console app approach you can have it called by making use of Process.Start for example.
If the purpose of your application is only to run a specific task every six hours, you might be better off creating a command line application and creating a scheduled task that Windows runs automatically. Obviously, you could then manually start this application.
If you're still convinced you need a service (and honestly, from what I've seen so far, it sounds like you don't), you should look into using a Timer, but choose your timer carefully and read this article to get a better understanding of the timers built into .NET (Hint: Pay close attention to System.Timers.Timer).
To prevent reentry if another method tries to call DoWork() while the process is in the middle of performing its operation, look into using either a Mutex or a Semaphore.
there are benefits and drawbacks either way. my inclination with those options is to choose the windows service because it makes your deployment easier. scheduling things with the windows task scheduler is scriptable and can be automated for deployment to a new machine/environment, but it's still a little more nonstandard than just deploying and installing a windows service. you also need to make sure with task scheduler it is running under an account that can make the webservice call and that you aren't going to have problems with passwords expiring and your scheduled tasks suddenly not running. with a windows service, though, you need to have some sort of checking in place to make sure it is always running and that if it restarts that you don't lose hte state that lets it know when it should run next.
another option you could consider is using nservicebus sagas. sagas are really intended for more than just scheduling tasks (they persist state for workflow type processes that last for more than the duration of a single request/message), but they have a nice way of handling periodic or time-based processes (which is a big part of long running workflows). in that a saga can request that it get back a message from a timeout manager at a time it requests. using nservicebus is a bigger architectural question and probably well beyond the scope of what you are asking here, but sagas have become how i think about periodic processes and it comes with the added benefit of being able to manage some persistent state for your process (which may or may not be a concern) and gives you a reason to think about some architectural questions that perhaps you haven't considered before.
you can create a console application for your purpose. You can schedule the application to run every 6 hours. The console will have a default method called on application start. you can call your routine from this method. Hope this helps!!
I'm writing a small app that needs to be executed once a week.
I could write it as a service that runs constantly but only executes the task that I need it to once a week, but that seems like overkill.
Is there another way of executing an application once every x period of time?
I'm looking for a solution that doesn't involve user interaction.
You can always use Windows Scheduled Tasks. They can be ugly, but effective.
You could use the Windows Task Scheduler. It was designed with this scenario in mind.
You can use Windows Scheduler for planning execute app once a week.
Under windows you can use the at utility.
You can use the Windows Task Scheduler in the Control Panel. Just set up a task to run your application on the specified day. You can even tell it to run as a certain user if you want.
If you need full control on the process (result values, parameters to be provided, etc), I would suggest you to create your own task scheduler, where you can use the cron algorithm to schedule the time you want execute your task.
I know it may sounds overkill to create a service to run once a week, but if you make a generic scheduler, you will be able to reuse your schedule for other purposes.
I created this scheduler in the past, and it has been used for years in production. I implemented the cron algorithm in .Net, based in the open source algorithm that one developer (Artif Aziz) wrote. Check out my blog for more information on this:
CronTab schedule parser algorithm
If you think in a higher level (enterprise level), you could consider buying Control-M, one of the most powerful scheduler tools I have ever used, however it is quite expensive.
Cheers!
Roberto.