Creating a scheduler in C# to connect PublishThis API - c#

I want to create a scheduler which does following
Setup a scheduler that loops every 5 minutes
Inside of this scheduler, the following is done:
If there is a timestamp stored from a previous loop, use it
If scheduler is running for the first time, take the current time into a timestamp value
Call the method http://webapi.publishthis.com/rest/mixes/mixTemplates/?token=TOKEN:E4/TOrnj0kII13i7un7wnpTY7NSbBEPHNic8RAQKbkaB+siGaxk467A==
Can you please help to achieve it?

The simplest answer is to make a console program, and just set it as a scheduled task that runs every 5 minutes.
If you have to make it part of the web project, then there are other questions about doing that:
How to schedule a C# code execution
Whats the best way to run a time consuming script in ASP.NET at regular intervals on shared hosting?
Schedule task in ASP.NET

Related

Creating a scheduled task that runs an algorithm daily to check whether an item has expired

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.

Run method at specific time MVC application

I'm developing a MVC application and it has a method, which should have to run end of the day. I do not have the access to windows task scheduler and implementing windows service is not possible. (Because deployed package should have to give to different customers and they have their own servers. In this case we not gonna deploy windows service or use task scheduler)
Please tell me is there any method to execute some method at specific time.
I saw there are methods which are use timer. But the problem is this job should have to do only one time in the day and process will not take longer than 1 minute.
If we use timer, resources will allocate to timer service and it is not a good method as i guess, because this method run only one time in the day. (Process is running in other 23 hours and 59 minutes and it is wasting resources. I guess timer concept is ok, if process is running in every 5,10 minutes)
Please give me direction....
the best way is to use Quartz.Net
this is a good example to use it :
http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net
I suppose that you already have a database. Create a table with a single datetime field named LastRun. Save in this field the last date and time your method has been executed.
In your MVC application on Session Start or on Application start compare LastRun datetime with current datetime and decide if you are going to perform the above mentioned method. After the method has been executed update the LastRun value.

c# schedule tasks on random time?

I have many tasks (recorded in a database with scheduled time) that need to be run on different times on different dates randomly, and each task will take a long time to process.
Every week there will be new more tasks coming up. What is the best solution to automatically execute all these tasks at an exact time?
Using an ASP.NET application to execute recurring background tasks is usually a bad idea as Phil Haack explained in his blog post.
So you could write a console application that will query your database and perform the necessary tasks. Then simply use the Windows Scheduler to execute this application at the required intervals. As an alternative you could implement the task in your ASP.NET application and have the console application simply fire an HTTP Request to the corresponding controller action.
If you don't have access to your server and cannot host anything else than an ASP.NET application then you should be perfectly aware of the consequences of going with this approach (as explained in the aforementioned article). So one approach would be to use a library such as Quartz.NET to schedule the execution of some code at regular intervals.

how to create and run cron job in asp.net page to run every 30 minutes

I need some guidance on creating and running a Cron Job in asp.net(C#.net) to run my page every 30 minutes. My Web page has to load data from Sql Server to MySql database every 30 minutes with this cron job.
Thanks
I don't think this is best suited for ASP.NET. You might want to just create a regular Console App, configure it in the Server Task Scheduler to run every 30 minutes. Or if you so inclined, you can create your own Windows Service to do this.
I don't think you'll find that feature out-of-the-box in ASP.NET. Here's what I usually do...
Setup a database table to track jobs, including the last time that each job ran.
In Global.asax's Application_Start event, I spin up a new .NET Thread, which runs a job checking loop.
In this loop, check your known jobs, check the database for the last run time, and then decide if it's time to run the job again. Run the job, update the database.
Sleep the thread for a bit. Continue the loop.
Be aware of the what-if's.
What if a task fails in your job loop, throwing an exception.
What if your in a server farm, and need to prevent multiple application instances from running the same task at the same time.
This is just something to get you started. Making this robust (if you need it robust) can get complicated.
Try investigating quartz.net, which is an open source port of the quartz project. I believe you should be able to include it as part of your web application.

Windows service that will run every hour

I m able to build a windows service and install it.
I m curious how can i run this service every hour ? I want it to run every hour periodically.
I also need to know the hour range that it s running so that I can store it somewhere.
How can i do that?
Edit : This service will be installed on many machines, therefore, I dont want to create a scheduled task say on 100 servers.
If you want a task to run on a regular interval as opposed to constantly, you should look into using the Task Scheduler.
If you need your code to be a service, but to be "activated" every hour, the easiest approach would be to make your service a COM object and have a simple task scheduled every hour that invokes a jscript/vbscript that creates your COM object and calls simple method on it.
The alternative is to use any of the wait APIs to "waste" an hour without consuming cycles.
Note that you also have to consider some interesting design decisions that depend on what your scenario is:
how is your service going to be started if it crashes or is stopped by the user?
if you are started after more than an hour, should you run again or do you need to wait to get on the exact hourly schedule?
how do you keep track of the last "activation" time if the timezone or the day-light saving time has changed while you were not active?
does your service prevent the computer from going to sleep/hibernate on idling or when the laptop cover is closed? if not, do you need to awake the computer on the hour to get your service working on your schedule?
Some of those are taken care of by the task scheduler, so I would strongly recommend going that route vs. waiting for an hour in your code.
You could create a scheduled task that runs every hour, to either run the service or send a message to "wake it up". Then, either pass in the current time in the scheduled task request, or just have your program pick up the current time when it wakes up.
Task Scheduler Managed Wrapper can help you set this up programmatically; you can google for other resources as well.
There are a couple options.
You could sleep for an hour.
You might be better suited for a Scheduled Task, not a service.
Thread.Sleep(1000*60*60);
Thread.Sleep(TimeSpan.FromHours(1));
code more readable this way
Thread.Sleep() solution will make sure that your service will run in one hour intervals, not every hour i.e. each task will be started at 1 hour + time to run the task. Consider using a Timer within your service. This will be a more robust solution since you have a control when to run a task, monitor its progress etc. Just remember that each Timer event will be fired in a different thread and if the task takes longer than one hour to run you might have to wait for the first task to finish to avoid concurrent tasks.
Task schedulers may be a good idea but services are designed to do this. Services gets installed easily and logs things properly. All you need to do is, at start of service, you can install a system timer (System.Threading.Timer) or there is also one more timer.

Categories

Resources