web网站里面,需要每隔1分钟,执行一个任务,并且一直保持这个定时执行状态,可以用如下一个方法:

   1,Global.asax里面的 Application_Start ,发生在第一次请求网站的时候,网站关闭(iis关闭网站或者删除网站)

    在写这个Application_Start  里面的内容之前,先写个定时器:

public  class Time_Task
{
public event System.Timers.ElapsedEventHandler ExecuteTask; private static readonly Time_Task _task = null;
private System.Timers.Timer _timer = null; //定义时间
private int _interval = 1000;
public int Interval
{
set
{
_interval = value;
}
get
{
return _interval;
}
} static Time_Task()
{
_task = new Time_Task();
} public static Time_Task Instance()
{
return _task;
} //开始
public void Start()
{
if (_timer == null)
{
_timer = new System.Timers.Timer(_interval);
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true;
_timer.Start();
}
} protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (null != ExecuteTask)
{
ExecuteTask(sender, e);
}
} //停止
public void Stop()
{
if (_timer != null)
{
_timer.Stop();
_timer.Dispose();
_timer = null;
}
} }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

2,然后,再写Global.asax

public class Global : System.Web.HttpApplication
{ protected void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);
Time_Task.Instance().Interval = 1000*10;//表示间隔
Time_Task.Instance().Start();
} void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)
{
//在这里编写需要定时执行的逻辑代码
} protected void Session_Start(object sender, EventArgs e)
{ // 在新会话启动时运行的代码 } protected void Application_BeginRequest(object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ } protected void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码 } protected void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件 } protected void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

然后,只要第一次访问网站,就会每隔 10秒(自己定义) 执行定义的任务,可以在要执行的任务里面设置断点,然后调试...

asp.net web 服务器端全局定时执行任务的更多相关文章

  1. 使用ASP.NET实现Windows Service定时执行任务

    转载http://blog.csdn.net/yanghua_kobe/article/details/6937816 我们怎样才能在服务器上使用asp.net定时执行任务而不需要安装windows ...

  2. 如何让Asp.net Web Api全局预防Xss攻击

    一.概述 二.什么是XSS 三.预防方法 四.在WebApi中如何实现 在实现之前,需要了解ASP.NET WEB API的pipeline机制. 如上,可以采用多种方式进行参数的过滤 1.重写Del ...

  3. ASP.NET Web API 全局权限和异常处理

    转自:http://yangpei.appsp0t.com/post/aglzfnlhbmdwZWlyDAsSBUVudHJ5GLkXDA 正文之前先解决一个问题 Web Api XML序列化的问题 ...

  4. ASP.NET Web API 全局权限和全局异常处理

    在开发中,我使用json格式序列化,所以将默认的xml序列化移除 public static class WebApiConfig { public static void Register(Http ...

  5. ASP.NET Web Pages:全局页面

    ylbtech-.Net-ASP.NET Web Pages:全局页面 1.返回顶部 1. ASP.NET Web Pages - 全局页面 本章介绍全局页面 AppStart 和 PageStart ...

  6. HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象

    HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象 通过前面对“HttpController的激活”的介绍我们已经知道了ASP.NET Web API通过Ht ...

  7. asp.net web 定时执行任务 定时器 Global.asax

    web网站里面,需要每隔1分钟,执行一个任务,并且一直保持这个定时执行状态,可以用如下一个方法: 以下代码是 Global.asax.cs 的全部代码. using System; using Sys ...

  8. ASP.NET Web API 过滤器创建、执行过程(二)

    ASP.NET Web API 过滤器创建.执行过程(二) 前言 前面一篇中讲解了过滤器执行之前的创建,通过实现IFilterProvider注册到当前的HttpConfiguration里的服务容器 ...

  9. ASP.NET Web API 过滤器创建、执行过程(一)

    ASP.NET Web API 过滤器创建.执行过程(一) 前言 在上一篇中我们讲到控制器的执行过程系列,这个系列要搁置一段时间了,因为在控制器执行的过程中包含的信息都是要单独的用一个系列来描述的,就 ...

随机推荐

  1. nginx 增加 lua模块

    Nginx中的stub_status模块主要用于查看Nginx的一些状态信息. 本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定: ./configure –wi ...

  2. iptables禁止别人,允许自己

    这里举个例子,以ping为案例:禁止别人ping自己,但允许自己ping别人. [root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 8 ...

  3. spingboot @EnableScheduling

    springboot让开发更简单!springmvc中启用定时任务还得需要在xml中进行配置启用并且要配置扫描器,但是springboot只需要一个注解就可以. @EnableScheduling 无 ...

  4. 2、CDH 搭建Hadoop在安装(安装Cloudera Manager,CDH和托管服务)

    安装Cloudera Manager,CDH和托管服务 建议使用此过程为生产环境安装Cloudera Manager和CDH.对于非生产“易于安装”,请参阅安装概念证明群集. 在开始安装之前,请确保已 ...

  5. CSSの変数を使う

    この文章はhttps://developer.mozilla.org/ja/docs/Web/CSS/Using_CSS_variablesを参考します. これは実験段階の機能です.この機能は複数のブ ...

  6. git ssh免登陆,以及ssh config

    git去连接github或gitlab上的远程仓库,可以使用ssh方式,也可以使用git的账号密码登录 这里介绍使用ssh方式实现免登陆(第一步和第二步即可实现)   第一步:生成ssh秘钥 ssh- ...

  7. 【scrapy】关于xpath helper中能匹配,但是在程序里匹配为none的问题

    原本使用xpath helper匹配的时候还是好好的,然鹅,把这个.//*[@id='novelInfo']/table/tbody/tr[3]/td[1]/复制到代码里就不行了,debug的时候显示 ...

  8. jenkins权限控制

    一.插件安装 插件:Role-based Authorization Strategy版本:2.3.2 二.全局安全配置 进入Jenkins后点击系统管理进入全局安全配置 当插件安装好的时候,授权策略 ...

  9. 第四章 栈与队列(c2)栈应用:括号匹配

  10. 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...