【发布时间】:2025-12-15 16:25:01
【问题描述】:
如何在共享托管服务器上按照配置的计划时间执行各种任务(例如电子邮件警报/发送新闻信)?
【问题讨论】:
标签: asp.net scheduling scheduled-tasks
如何在共享托管服务器上按照配置的计划时间执行各种任务(例如电子邮件警报/发送新闻信)?
【问题讨论】:
标签: asp.net scheduling scheduled-tasks
这是一个 Global.ascx.cs 文件,我过去曾经做过这种事情,使用缓存到期来触发计划任务:
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 分钟触发一次您的操作,但这可以在代码中进行配置。
【讨论】:
这里的 Somone 通过在 global.asax 中创建线程来做到这一点。听起来他们在这方面取得了成功。我自己从未测试过这种方法。
在我看来,这将是一个比超载缓存过期机制更好的选择。
【讨论】:
您可以在共享主机上使用ATrigger 调度服务,没有任何问题。 .Net library 也可用于创建计划任务而无需开销。
免责声明:我是 ATrigger 团队的一员。这是一个免费软件,我没有任何商业目的。
【讨论】: