简体   繁体   中英

C# run code every 30 days

如果我要运行一些代码,也许每隔30天向我网站的用户发送一封电子邮件,该怎么做?

使用Windows任务计划程序来运行您的应用程序。

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.

如果它是特定于SQL Server的,则可以使用SQL作业

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM