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. 签名Cookie

    [签名Cookie] set-cookie时加上防篡改验证码. 如: user_name=alex|bj95ef23cc6daecc475de   防篡改验证码的生成规则可以很简单:md5(cooki ...

  2. Android 数据库框架总结(转)

    转自 http://blog.csdn.net/da_caoyuan/article/details/61414626 一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封装完 ...

  3. IDEA中配置Maven+spring MVC+tomcat

    一:配置Maven安装教程如下: http://blog.csdn.net/qq_32588349/article/details/51461182 实际安装过程中,如果按照教程配置如下属性,最后创建 ...

  4. CSS:opacity:0,visibility:hidden,display:none的区别

    CSS中opacity=0,visibility=hidden,display=none的时候,三者有什么区别呢?? 参考了stackoverflow的博客,才发现区别如下所示: Here is a ...

  5. Allowing GPU memory growth

    By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICE ...

  6. FormData的使用

    var formData = new FormData(); <form id="coords" class="coords" onsubmit=&quo ...

  7. 解决IDEA查看源码时提示:Library source does not match the bytecode for class的问题分析

    解决方法:

  8. 我的图片爬虫demo

    本来是根据scrapy教程写了一个下载图片的简易爬虫,但是搞了一天,死活下载不了图片. 然后换了一个网站,用原生的库写了一个图片爬虫. import urllib.requestimport osde ...

  9. 100-days: Five

    Title: Feel better now ? The rise and rise of the anxiety economy(焦虑经济) rise and rise 一直上升 anxiety n ...

  10. 22 【python】入门指南:函数

    #!/bin/python def test_func(): return "test_func" a = test_func() print(a) 输出结果: test_func ...