在ASP.NET网站安排的作业,而无需购买专用服务器 [英] Scheduled jobs in ASP.NET website without buying dedicated servers
本文介绍了在ASP.NET网站安排的作业,而无需购买专用服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能执行各种任务(如电子邮件警报/发送快讯)一个共享的主机服务器上配置的计划时间?
How can I perform various task (such as email alert/sending news letter) on a configured schedule time on a shared hosting server?
推荐答案
下面是我用来做这种过去的事情,使用高速缓存到期触发计划任务的Global.ascx.cs文件:
Here's a Global.ascx.cs file I've used to do this sort of thing in the past, using cache expiry to trigger the scheduled task:
public class Global : HttpApplication
{
private const string CACHE_ENTRY_KEY = "ServiceMimicCacheEntry";
private const string CACHE_KEY = "ServiceMimicCache";
private void Application_Start(object sender, EventArgs e)
{
Application[CACHE_KEY] = HttpContext.Current.Cache;
RegisterCacheEntry();
}
private void RegisterCacheEntry()
{
Cache cache = (Cache)Application[CACHE_KEY];
if (cache[CACHE_ENTRY_KEY] != null) return;
cache.Add(CACHE_ENTRY_KEY, CACHE_ENTRY_KEY, null,
DateTime.MaxValue, TimeSpan.FromSeconds(120), CacheItemPriority.Normal,
new CacheItemRemovedCallback(CacheItemRemoved));
}
private void SpawnServiceActions()
{
ThreadStart threadStart = new ThreadStart(DoServiceActions);
Thread thread = new Thread(threadStart);
thread.Start();
}
private void DoServiceActions()
{
// do your scheduled stuff
}
private void CacheItemRemoved(string key, object value, CacheItemRemovedReason reason)
{
SpawnServiceActions();
RegisterCacheEntry();
}
}
目前,这激发了每2分钟你的行为,但是这是在code配置。
At the moment, this fires off your actions every 2 minutes, but this is configurable in the code.
这篇关于在ASP.NET网站安排的作业,而无需购买专用服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文