C# run code every 30 days - c#

If I were to run some code, perhaps send an email, every 30 days to users of my site, how would that be done?

Use Windows Task Scheduler to run your application.

Options:
1) Console app that runs in Windows Task Scheduler
2) Windows Service
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx

Which framework are you working with? I have some pointers if you're developing with .Net 4.0 like I am.
If you have access to your machine where you can install services I would utilize the new and improved Workflow Service for this situation. What's nice about them is that Workflows can persists for minutes, hours, days, weeks, months, etc. lying "dormant" until delays time periods finish.
If it's an IIS machine (and if that's the route you would like to take) it is relatively simple to build it straight as a "Workflow Service" project/solution in VS 2010. From there you are presented with a designer and several workflow activities in the toolbar.
Add a flowchart activity to place your email activities inside of. Flowchart is ideal because it can initialize whatever you need to and decision flow can redirect backwards in direction (as opposed to the always forward moving sequence activity). What you need to do with your logic is up to you from there since your question doesn't provide a lot of details.
Now if you're using .Net 3.5 then I would think about refraining from building Workflows since you have to migrate when switching to 4.0 (WF 3.5 is NOT compatible with 4.0). With admin access to the machine you can install a Windows Service that contains a timer to fire code every 30 days (or however defined) as needed.

If it's SQL Server specific, you can use SQL Jobs.

If you want to do it with your ASP.NET app instead of a separate app, you have some options:
Since you want it to run every 30 days instead of daily, I recommend this method of using a schedule table instead of application variables or cache to schedule jobs:
You can setup a schedule table in your db and check when it was last updated in your global.asax. On the Session start, check if the current date is more than 30 days. If it is, then call a method to send out emails.
If you send out emails, then update your schedule table with the current date.

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..

Scheduling methods to run in ASP.NET

Explanation:
I am developing a simple car business system and I have to implement the following feature:
A very special car model is delivered to a shop. There are a lot of people on waiting list exactly for this model.
When the car arrives the first client receives the right to buy it, he / she has 24 hours to use this opportunity.
I have a special state in the DB that determines if the user is: on waiting list (I have the exact position, as well) or can use opportunity to buy the car. Whenever the car arrives, I run a method that changes the state of the first client on waiting list. And here comes the problem:
Problem:
The client can use his opportunity, during the 24 hours period. But I have to check at the end, if he/she has bought the car. For this reason, I have to schedule a method to run in 24 hours.
Possible solution:
I am thinking about two things. First is using a job scheduler like Hangfire. The problem is that since I do not have any other jobs in my app, I do not want to include a whole package for such a small thing. Second is using making the checking method asynchronous and making the thread sleep for 24 hours before proceeding (I do not feel comfortable in working with threads and this is just an idea). I got the idea from this article. Keep in mind that more than one car can arrive in more than one shop. Does it mean that I should use many threads and how it is going to affect the performance of the system?
Question:
Which of the two solutions is better?
Is there another possibility that you can suggest in this particular case?
I agree. Importing a package for only one job if you aren't going to use it for many jobs is a little bit of overkill.
If you are running SQL server, I'd recommend writing a .NET console application to run on a schedule using the SQL Server Agent. (see image) If you have stored procedures that need to run, you also have the option to run them directly from the SQL job if for some reason you don't want to run them from your .NET application.
Since it sounds like you need this to run on a data driven schedule, you may consider adding a trigger to look for a new record in your database whenever that "special" car is inserted into the database. MSDN SQL Job using Trigger
I've done something similar to this where every morning, an hour prior to business hours starting, I run a .NET executable that checks the latest record in table A and compares it to a value in table B and determines if the record in table A needs to be updated.
I also use SQL Server to run jobs that send emails on a schedule based on data that has been added or modified in a database.
There are advantages to using SQL server to running your jobs as there are many options available to notify you of events, retry running failed jobs, logging and job history. You can specify any type of schedule from repeating frequently to only running once a week.

MVC .NET Web app that does something every week

I am building a Web application using MVC3 .NET
The app should somehow keep a weekly todo list for each user. On Fridays, all todo lists must be reset.
Can someone help with that.
thanks
You may take a look at Quartz.NET. This being said, the culprit of handling scheduled jobs by a web application is that the web server could unload this ASP.NET application under some circumstances (memory or CPU threshold is hit, the application is not used for a long time, ...). For this reason it is recommended to develop a separate process that will perform those tasks. This process could be hosted for example in a Windows Service which is guaranteed to run all the time.
Another possibility is to simply use the Windows Scheduler in order to launch some custom made executable on a weekly basis which will take care of performing the necessary tasks.
If this is some database-level task, then perhaps a scheduled-task (or crontab on nix). There is no need for MVC to do that. If you really want, the scheduled-task could just hit a route on the web-site.
However, personally I'd simply partition the data with a week-number (or a start date), so that when I get the data, I'm getting the data for the week starting (your date). Then:
there is nothing to do
you have access to history as needed
i.e. add StartDate (or similar) as a column on your existing storage, and use that to select the most timely data. You will get calls about "I need the data from last week" - you might as well built it in from the outset. Besides... if something isn't done on Friday, that doesn't mean it ceases to exist...
do you expect the application to always be up and running? what if nobody is using it on midnight between Thursday and Friday? How can you then execute those jobs?
in general since a web application is not surely running at any given time, you cannot rely on timers inside the web application.
I think best solutions in this case would be to either create a windows service which will be installed on the server and will be always running by definition... a bit like reinventing same things again and again...
or you could also set a scheduled task in windows to call your web application, creating a request to a specific page every Friday, then the web application gets invoked with some parameters and you can do what you need.

How to send automatic email on daily ,weekly and monthly bases?

How do I send email on daily, weekly and monthly bases using c#.net?
I was thinking of creating a windows service application but I don't know how to do it and if it's the right way to take.
Your thoughts are greatly appreciated.
A windows service is probably the best option.
In your service have a timer that fires with whatever resolution you need (every minute/hour/day etc) and on the timer tick even send your emails.
There are many tutorials for creating windows services with .NET.
A Windows service will do the trick. A good benefit of a Windows service is that it starts up when Windows starts up (or can be set to, anyway). So the machine can be left fairly unattended (as a good server should) and doesn't need anybody logged into it for the service to run. So if that's an important consideration for your scenario then perhaps a Windows service would be the way to go.
If you just want to create a console application instead (which can generally be easier to create/test/debug) then you can schedule it with the native Windows task scheduler. However, unless I'm mistaken, I think a user needs to be logged in to the machine in order for it to run. At least under certain circumstances. So a Windows service is probably your best bet for an unattended task.
As for creating the service, Visual Studio should have a project template for that. The scheduling would be handled with a Timer.
You could also use the Windows Task Scheduler. Quartz.Net seems to be the right tool, too (never used it though).
Windows Service is a good choice.
Remember to save state to disk (file or database) because service restart (for example, due to a reboot) is possible. For example, your next email delivery is a week in the future, you have to save that date to disk. So when your server is down due to a power failure 3 days later and recover after another day, your Windows Service can still set the right timer according to the date saved to disk.
Create a console application, which would be launched from the Windows task scheduler. Creating a windows service is unnecessarily complex, IMO, since you are simply having the program execute in response to time intervals; not other unpredictable external events where you would need a 'listener' type application. KISS!

.NET Windows Service - Architectural Decisions

I currently have a Windows Service which continually runs throughout the day. It has multiple threads which kick off:
tasks daily to update cache
tasks weekly to do cleanup
24/7 task to import XML into SQL Server
tasks which run for around 12 hours per day kicking off a console application to manage ETL
The tasks are not the important part of this question but it gives you the idea that this Windows service has grown to be a monster. It manages the imports of somewhere in the region of 300 million records per day.
It is hectic, but it works.
This iteration of development is giving me a chance to review the service and possibly break it down so that it is more manageable. It is thought that this could be multiple services with one manager service - ideal if a component needs to be updated then the whole thing does not need to grind to a hault.
Does anyone have any experience with this? I am keen to hear about your approach because it is new territory for me.
I have read this SO post which touches on the topic.
Cheers.
Your description sounds a lot like the thing I wrote about 2 years ago. A windows service which hosts addins and runs them in multithreaded environment. The architecture used is the .NET addin pipeline introduced in .NET 3.5 (System.AddIn-namespace). It works like a charm as I also integrated live/hot updates. It's so easy to implement new addins and just plug them in whenever I like. So I really recommend using this addin stuff.
See http://msdn.microsoft.com/en-us/library/cc175292.aspx for a quickstart.
I've done something similar for our background services, there's basically a ServiceHost and "Servlets" which are loaded via appdomains so they don't impact each other.
Why a service? Pretty much none of the things you do- except the 24/7 task for imports - are something I would do as a service.
I would use:
Either command line programs that get regularly scheduled, especially on the daily / weekly tasks
Or use SSIS and SQL Scheduler to schedule something.
The only thing that may justify a service is the 24/7 XML import - unless you can get away starting it, for example, every 5 minutes.

Categories

Resources