Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer to throw Method Once a Week - C#, Asp.NET

Tags:

c#

asp.net

timer

I'm fairly new to C# and coding in general. I'm looking for an example/open source Timer Function that will be used to throw a basic method (ex. Email Notifications) once a week at a certain time. After doing some research I've found that using a Timer instead of a Windows Service would be wise as it will have a very small workload.

I've found a couple timer API's on CodePlex and Code Project but am having trouble working my way through the Examples due to my minimal knowledge of C#.

Does anyone know of a Simple timer that I could use that is Beginner friendly and that also has a walk through? Or is there a simpler way of doing this using System.Timers?

My end goal is to have a timer that will fire a Method at 8:00AM every Monday.

EDIT: I believe I should have been more detailed in my initial Post. The reason I chose not to use the Windows Task Scheduler is because the Method I am invoking is more complex than just invoking Windows Task.

When the timer hits the scheduled time it will fire a Method which queries the DB for Items requiring service within a certain time-frame. This will then loop through the items adding them to an email which will be sent to that Warehouse's Admin.

If I'm not mistaken, that cannot be done through Windows Task Scheduler?

like image 544
Lando Avatar asked Dec 16 '22 16:12

Lando


2 Answers

Using a timer for this makes no sense. Your goal is to have a function fire at a specific time. What if your server reboots? Is your timer going to calculate the exact time between the reboot time and Monday at 8:00 am?

IMO, use a console application and the Windows Task Scheduler.

Response to edit: Of course you can use Windows Task Scheduler for this. The only function of the scheduler is to execute a task. There are no rules as to the complexity of the task it executes. Plus, querying a DB and sending an email isn't exactly an overly complex task. :)

like image 194
Scott Avatar answered Jan 04 '23 06:01

Scott


I use Quartz.NET for cron jobs. This works on a hosted platform as well, where you have no access to windows services.

What you are looking for would be trivial to achieve. Look at the first example from their tutorial.

http://quartznet.sourceforge.net/tutorial/lesson_1.html

like image 41
santiagoIT Avatar answered Jan 04 '23 08:01

santiagoIT