Automatic shutdown of operations / actions

We need to send newsletters / messages to our users at a specific time. This is done online; however, we want this to be done automatically over time. Although this problem is suitable for a model such as Window Service, we are placing it in the public domain; therefore, we cannot use this option.

Is there a way that this particular action can be called automatically at a specific point (time) specified in the configuration in web.config.

This is currently being done manually, invoking the operation through the admin panel.

Platform - ASP.NET 3.5
Language - C #

+4
source share
3 answers

There is a simple solution to do things "every X units of time." Although this is not exactly what you are looking for, it is surprisingly easy to implement, so this may be the right solution. (You can run the task every X minutes, which does some work if the current time is within some given time window.)

http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

The main idea is this (as quoted from this link):

  • At startup, add an item to HttpRuntime.Cache with a fixed expiration.
  • When the cache item expires, do your job like WebRequest or whatever you have.
  • Re-add the item to the fixed expiration cache.

By the way, this method was used to assign icons here in Stackoverflow. :-)

+2
source

This can be done using System.Threading.Timer , but you have to be very careful how you do it. You may run into all the problems if you just drop the timer just like in the Windows service. The Windows service works as long as it works, and is single-threaded if you do not make it multithreaded; ASP.NET AppDomain is neither one nor the other.

This article has good information on how to properly protect the timer. Highlights:

  • You need to serialize timer calls - do not allow re-inclusion in the callback if one callback is already running, and then skip the next one,

  • You must properly manage the timer if the AppDomain is unloaded, which can happen for various reasons.

So, continue to use System.Threading.Timer in your Application_Start method, but keep these issues in mind and write the appropriate protections to the code.

+1
source

Here is an example of using a timer in an ASP.NET application. You can check after the timer is triggered (or the cache expires if you go with this option) and see if you have passed the past time in web.config to trigger your event.

http://www.eggheadcafe.com/tutorials/aspnet/223319d8-6366-492a-8eae-e3c7a26c88a4/refreshing-aspnet--cache.aspx

Global.asax code (you must register System.Threading.Timer):

 public class Global : HttpApplication { private static Timer timer; private int interval = 60000*5; // five minutes protected void Application_Start(object sender, EventArgs e) { if (timer == null) { timer = new Timer(new TimerCallback(ScheduledWorkCallback), HttpContext.Current, interval, interval); ScheduledWorkCallback(HttpContext.Current); } } public static void ScheduledWorkCallback(object sender) { //do your work here } } 
0
source

Source: https://habr.com/ru/post/1300532/


All Articles