服务器时间为24:00:00或(12 pm)时如何每天运行计划过程 [英] how to run schedule process every day when server time is 24:00:00 or(12 pm)
本文介绍了服务器时间为24:00:00或(12 pm)时如何每天运行计划过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨...朋友
我正在global.asax中编写代码,我想每天在服务器时间为24:00:00或(中午12点)运行一次代码,以便我在Application_Start中通过哪种条件?
如何使用global.asax执行此任务?
我的网站已完全托管在服务器上.
hi...friends
i am write code in global.asax i want to run code every day once at time when server time is 24:00:00 or(12 pm) so which condition i pass in Application_Start???
how to perform this task using global.asax?
my website allready hosted on server.
推荐答案
@ bhavesh002您可以在asp.net Global.asax
中进行操作
我在这里提供代码,您可以使用该代码执行所需的操作
使用此日程表类
@bhavesh002 You can do it in asp.net Global.asax
I am providing here code by using which you can perform your desired action
using this schedule class
public class Scheduler
{
private class CacheItem
{
public string Name;
public Callback Callback;
public Cache Cache;
public DateTime LastRun;
}
public delegate void Callback();
private static int _numberOfMinutes = 1;
public static void Run(string name, int minutes, Callback callbackMethod)
{
_numberOfMinutes = minutes;
CacheItem cache = new CacheItem();
cache.Name = name;
cache.Callback = callbackMethod;
cache.Cache = HttpRuntime.Cache;
cache.LastRun = DateTime.Now;
AddCacheObject(cache);
}
private static void AddCacheObject(CacheItem cache)
{
if (cache.Cache[cache.Name] == null)
{
cache.Cache.Add(cache.Name, cache, null,
DateTime.Now.AddMinutes(_numberOfMinutes), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, CacheCallback);
}
}
private static void CacheCallback(string key, object value, CacheItemRemovedReason reason)
{
CacheItem obj_cache = (CacheItem)value;
if (obj_cache.LastRun < DateTime.Now)
{
if (obj_cache.Callback != null)
{
obj_cache.Callback.Invoke();
}
obj_cache.LastRun = DateTime.Now;
}
AddCacheObject(obj_cache);
}
}
在这里,您应该在global.asax
中编写这样的代码
Here you should write your code like this in global.asax
protected void Application_Start(object sender, EventArgs e)
{
if(DateTime.Now.TimeOfDay == Yourtime;)
Scheduler.Run("test", 20, RunScheduleTasks);
}
如果有帮助,请标记为答案
Mark as answer if it helps
Application_Start出于其他目的而存在.您不能使其运行预定的作业.他们是安排工作的多种方式.如果它是以数据库为中心的作业,则数据库必须具有计划作业的机制. (我知道SQL Server有一个).您也可以使用Windows Server Task计划程序或任何其他第三方计划程序.
Application_Start exists for a different purpose. You cannot make it run a scheduled job. They are various ways to schedule jobs. If it is a database-centric job, your database must have a mechanism to schedule jobs. (I know SQL Server has one). You could also use Windows Server Task scheduler or any other 3rd party scheduler.
这篇关于服务器时间为24:00:00或(12 pm)时如何每天运行计划过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文