环境:.net4.5

需求:需要一个方法定时执行任务

解决: System.Threading.Timer 提供以指定的时间间隔执行方法的机制。 此类不能被继承,有10多种实例化方法,满足多种情况.

步骤:委托方法,注册执行

1.代码主体
 public class CensusdemoTask
{
System.Threading.Timer timer;
private static int count = ; public CensusdemoTask()
{
timer = new System.Threading.Timer(SetCensusURL, null, , * );
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void SetCensusURL(object obj)
{
string txt = string.Format("写入时间:{0},次数{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), count);
FileInfo f = new FileInfo("/124.txt");
StreamWriter sw = File.Exists("/124.txt") ? f.CreateText() : f.AppendText();
byte[] txtbytes = Encoding.UTF8.GetBytes(txt);
sw.WriteLine(txt);
sw.Flush();
sw.Close();
count++;
}
}

2.注册 Global.asax.cs

 protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//注册,
BLL.SystemTask.CensusUrlTask t = new BLL.SystemTask.CensusUrlTask(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); // DependencyResolver.SetResolver(new NinjectDependencyResolver());
//ControllerBuilder.Current.SetControllerFactory(new NinjectDependencyResolver());
}

MVC 定时执行任务的更多相关文章

  1. 基于ASP.NET MVC定时执行任务调度

    相对FluentScheduler实现定时调度任务的使用简单,配置少的特点,Quartz.Net则配置稍微复杂一些.下面我们就接合一个 ASP.NET MVC网站应用程序的定时执行任务调试的小实例来了 ...

  2. mvc定时执行任务(获取气象台的气象数据,定时新增)

    1.定时任务: gloabl.asax文件Application_Start()方法注册: System.Timers.Timer t = new System.Timers.Timer(theInt ...

  3. ASP.NET MVC计划任务实现方法(定时执行某个功能)

    系统中定时执行某个任务是比较常用的功能,如一个部门定期向上级部门上报数据是一个典型的例子,下面就简单说说在.net mvc中如何实现定时执行某个功能的方法. 1.首先修改Glocal.asax文件,在 ...

  4. webapi + windows计划 + mshta 实现定时执行任务

    当然,实现定时任务有更好的操作方式,比如方式一:asp.net mvc+quartz.net +corn +webapi,asp.net mvc做任务管理的平台,使用CronTrigger做定时触发, ...

  5. ASP.NET MVC4计划任务实现方法(定时执行某个功能)

    系统中定时执行某个任务是比较常用的功能,如一个部门定期向上级部门上报数据是一个典型的例子,下面就简单说说在.net mvc中如何实现定时执行某个功能的方法. 1.首先修改Glocal.asax文件,在 ...

  6. Quartz.Net实现的定时执行任务调度

    在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler>和<简单.轻量.功能非常强大的C#/ASP.NET定时调度 ...

  7. C#定时执行

    代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  8. 【转】linux 定时执行shell脚本

    在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程,在像备份等需要在操作系统级定时任务只能采用crontab来完成 本文讲述crontab具体用法,以供备忘. 在oracle ...

  9. Linux下定时执行脚本(转自Decode360)

    文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog  老师(业精于勤而荒于嬉 ...

随机推荐

  1. maven+springmvc+dubbo+zookeeper

        为什么要用dubbo?   还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/   一般 nginx+tomcat ...

  2. error: failed to push some refs to 'https://github.com/github账号/learn_git.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caus

    在使用git 对源代码进行push到gitHub时可能会出错,信息如下   此时很多人会尝试下面的命令把当前分支代码上传到master分支上. $ git push -u origin master ...

  3. jQuery中的100个技巧

      1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. < ...

  4. 五.Jenkins安装plugin

    http://blog.csdn.net/jmyue/article/details/9376237

  5. PL/SQL循环

    1.if循环做判断 SET SERVEROUTPUT ON accept num prompt 'qinshuu'; DECLARE pnum NUMBER :=& num ; BEGIN T ...

  6. POJ 1815 Friendship

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10626   Accepted: 2949 Descr ...

  7. apche启动错误|httpd.pid overwritten — Unclean shutdown of previous Apache run?

    APACHE启动成功,但无法接受任何请求,查看ERROR.LOG文件[warn] pid file /opt/apache/logs/httpd.pid overwritten - Unclean s ...

  8. MySQL外键约束

    mysql的五种约束 1.PRIMARY KEY 2.UNIQUE KEY 3.NOT NULL 4.DEFAULT 5.FOREIGN KEY 其中外键约束的使用用法 CREATE TABLE te ...

  9. MVC Controller中View(model)如何在 View中的index页面获得?

    http://bbs.csdn.net/topics/390723984?page=1 在页面顶部定义@model List<UserModel>使用@foreach(var x in M ...

  10. 个人整理的一些web前端面试题

    1.一个网页从开始请求到最终显示的完整过程? 获取输入网址 将网址解析为ip地址 建立tcp连接 发送http请求 返回响应 解析并显示. 2.简述对CSS盒子模型的理解? width=margin- ...